简体   繁体   中英

How do you change the color of unselected items in a Tab Bar?

I'm trying to change the color of the unselected item images in my Tab Bar from the default gray color to the actual color of the image (white), but my efforts have been unsuccessful. There are several threads related to this question on Stack Overflow, but many of them seem to be outdated.

Tab Bar with Home and Profile icons

As you can see from the picture, the unselected Profile icon on the right has a default gray color. After doing some research, I've tried running the solution below in the App Delegate file to change its color to white, but sadly this hasn't worked. I'm a novice Swift programmer (incidentally, this is also my first post), so any help or suggestions would be greatly appreciated.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    let tabBarController = UITabBarController()
    tabBarController.tabBar.unselectedItemTintColor = .white

    return true
}

Make the icon images Black, if they are white Im not sure it will work. As in the actual image themselves should be black in the assets folder not white.

If you add a tabbar from the story board you can put these line of code in that method in the appDelegate method shown in your post

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

You can try to use UITabBar.appearence(). unselectedItemTintColor, more information here https://developer.apple.com/documentation/uikit/uitabbar/legacy_customizations

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