Swift - 无法更新 UITabBar barTintColor

Swift - Can't update UITabBar barTintColor

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 中如此难以实现?将不胜感激。

I can do this with objective-c by using [[UITabBar appearance] setBarTintColor:[UIColor blackColor]];

嗯,Swift 相当于 UITabBar.appearance().barTintColor = UIColor.black。 (我不知道为什么你的代码都指的是white,如果你想要的是black。)

您说的是 Swift 而不是 Objective-C 这一事实使得此功能 没有 差异。如果这在您的应用程序的 Objective-C 版本中有效但在 Swift 版本中无效,那么您正在做一些 else 与 Swift 版本不同的事情— 你没有透露给我们的东西。

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

方法一:

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

输出:

方法二:

注意:我将 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

输出: