【问题标题】:How do I change the color of the tab bar for one view controller?如何更改一个视图控制器的标签栏颜色?
【发布时间】:2017-08-23 21:02:39
【问题描述】:
我的标签栏的应用程序委托中有一个白色的栏色调,但我需要它在所选索引之一上清晰。我将以下代码添加到需要清晰的视图控制器中,但它保持清晰。我如何拥有它,所以这只适用于一个标签。
let tabBar = self.tabBarController?.tabBar
tabBar?.barTintColor = UIColor.clear
tabBar?.backgroundImage = UIImage()
tabBar?.shadowImage = UIImage()
【问题讨论】:
标签:
ios
swift
uitabbarcontroller
【解决方案1】:
如果你有 UITabBarControllerDelegate 你可以使用
func tabBarController(_ tabBarController: UITabBarController,
didSelect viewController: UIViewController) {
guard let index = self.viewControllers?.index(where: { $0 == viewController }) else {
return
}
if index == 1 {
self.tabBar.barTintColor = .black
} else {
self.tabBar.barTintColor = .yellow
}
}
我正在按索引检查,因为我有导航控制器,这对我来说更容易,你可以尝试使用
if viewController is MyViewController {
self.tabBar.barTintColor = .black
} else {
self.tabBar.barTintColor = .yellow
}
【解决方案2】:
试试这个。在一个选项卡上设置 UIColor.clear 并在另一个选项卡上设置不同的颜色。
let tabBar = self.tabBarController?.tabBar
let tabBarItems = tabBar?.items
if(tabBar?.selectedItem == tabBarItems?[0])
{
tabBar?.barTintColor = UIColor.clear
}
else
{
tabBar?.barTintColor = UIColor.red
}