如何明确设置 UITabBar 背景颜色和色调颜色

How to definitively set UITabBar background color and tint color

我已经尝试设置 UITabBar 的色调和背景颜色很长一段时间了,但似乎没有任何效果。 到目前为止我已经尝试过:

tabBarController?.tabBar.backgroundColor = UIColor.orangeColor()
tabBarController?.tabBar.barTintColor = UIColor.whiteColor()

以及:

UITabBar.appearance().tintColor = UIColor.orangeColor()

这些似乎都没有对我的标签栏产生任何影响。我还想提一下,我将 VC 嵌入到导航控制器中,为此我设置的全局色调效果非常好。

设置标签栏背景颜色with barTintColor:

self.tabBar.barTintColor = UIColor.blueColor()
//or
UITabBar.appearance().barTintColor = UIColor.blueColor()

标签栏色调颜色:

self.tabBar.tintColor = UIColor.whiteColor() // Selected tab color
//or
UITabBar.appearance().tintColor = UIColor.whiteColor()

如果你想隐式设置标签栏的 tint and barTint color 那么在你的 Appdelegate.swift,

    UITabBar.appearance().barTintColor = .orange
    UITabBar.appearance().tintColor = .green

如果你想为特定的 viewController 设置标签栏的 tint and barTint color 那么在 ViewController.swift,

 self.tabBarController?.tabBar.tintColor = .orange
 self.tabBarController?.tabBar.barTintColor = .green

Swift 4+ 版本

UITabBar.appearance().barTintColor = UIColor.red
UITabBar.appearance().tintColor = UIColor.white

与 UINavigationBar 在 iOS 15 上没有内容时默认透明的方式类似,UITabBar 的工作方式相同。这可能是您免费获得的一个很好的视觉刷新(因为一旦您使用 Xcode 13 构建它就会默认打开)或者它可能会给您的应用程序带来很多问题。

if #available(iOS 13.0, *) { 
    let tabBarAppearance: UITabBarAppearance = UITabBarAppearance() 
    tabBarAppearance.configureWithDefaultBackground() 
    tabBarAppearance.backgroundColor = UIColor.tabBarBackground 
    UITabBar.appearance().standardAppearance = tabBarAppearance 
}
if #available(iOS 15.0, *) { 
    UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance 
}