ios - Swift - 无法更新 UITabBar barTintColor

标签 ios objective-c swift uitabbarcontroller uitabbar

viewDidLoad 中,我尝试了以下方法来更新我的 UITabBar 的背景或“barTintColor”。我可以通过使用 [[UITabBar appearance] setBarTintColor:[UIColor blackColor]]; 用 objective-c 做到这一点。

我尝试过的:

    UITabBar.appearance().barTintColor = UIColor.white

    self.navigationController?.navigationBar.barTintColor = UIColor.white;

    UINavigationBar.appearance().barTintColor = UIColor.white

    UINavigationBar.appearance().backgroundColor = UIColor.white

我想避免使用一些奇怪的 NSNotificationCenter 解决方案并从 AppDelegate 进行更新。为什么这在 Swift 中如此难以实现?将不胜感激。

最佳答案

注意:下面的方法应该在 Swift 3 中工作。下面的代码必须进入你的 viewDidLoad 才能工作。

方法一:

    tabBarController?.tabBar.barTintColor = UIColor.white
    tabBarController?.tabBar.tintColor = UIColor.red
    tabBarController?.tabBar.isTranslucent = false

输出:

enter image description here

方法二:

注意:我将 barTintColour 设置为绿色以显示这两种方法都有效..

    UITabBar.appearance().barTintColor = UIColor.green // Its strange why this method didn't worked for you?.Try updating your post with viewDidLoad.Its a better way to understand the issue.
    UITabBar.appearance().tintColor = UIColor.purple

输出:

enter image description here

关于ios - Swift - 无法更新 UITabBar barTintColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40579961/

相关文章:

swift - 如何访问 Parse 的 getObjectInBackgroundWithId block 之外的变量值

使用dispatch_semaphore时iOS 14崩溃僵尸

ios - 使用自动布局、键盘插入和横向旋转配置 UIScrollView 内容

iphone - 删除应用程序后按下 CFUUIDRef

ios - 在 Swift 4 中从另一个 View Controller 获取数据(不是通过向前或向后传递数据)

java - Swift 解码和编码字符串

ios - 隐藏 UITextField 中的选择句柄

ios - 函数返回值错误(可能是错误的字符串 "type"?)

ios - iPhone Multiplayer Code 有效,但 iPad 无效。委托(delegate)问题?

objective-c - iOS iPhone 获取一对多实体