繁体   English   中英

如何更改一个视图控制器的选项卡栏的颜色?

[英]How do I change the color of the tab bar for one view controller?

我的标签栏的应用程序委托中有白色的条形,但我需要在选定的索引之一上使其清晰。 我将以下代码添加到需要清晰的视图控制器中,但仍保持清晰。 我怎么拥有它,所以这仅适用于一个选项卡。

let tabBar = self.tabBarController?.tabBar
tabBar?.barTintColor = UIColor.clear
tabBar?.backgroundImage = UIImage()
tabBar?.shadowImage = UIImage()

如果您有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
}

尝试这个。 在一个选项卡上设置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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM