简体   繁体   中英

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

I have a bar tint of white in app delegate for my tab bar, but I need it to be clear on one of the selected indexes. I add the below code to the view controller that needs to be clear, but it stays clear. How do I have it so this only applies to one tab.

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

If you have UITabBarControllerDelegate you can use

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
    }
}

Im'm checkig by index because i have navigation controller and this is easier for me, you can try to use

if viewController is MyViewController {
    self.tabBar.barTintColor = .black
} else {
    self.tabBar.barTintColor = .yellow
}

Try this. Set UIColor.clear on one tab and a different color on other tab.

let tabBar = self.tabBarController?.tabBar
let tabBarItems = tabBar?.items
if(tabBar?.selectedItem == tabBarItems?[0])
{
    tabBar?.barTintColor = UIColor.clear
}
else
{
    tabBar?.barTintColor = UIColor.red
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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