简体   繁体   中英

TabBar item tint color does not change

I have a tab bar with 5 tabs. I have put different images for selected and unselected state of the tab bar items .

No matter what I do, the tint color does not change and it does not adapt the image color.

When the tab is selected, the color should be black and when unselected, it should be orange.

Here is an image of the attribute inspector with the images assigned.

在此处输入图片说明

Image of the tab bar

在此处输入图片说明

How do I change the image color?

The problem is that you can't control the tint color of the unselected items. That's not the fault of your code; it's just how iOS works. This used to be possible, but at some point (iOS 7? can't remember) it just went away.

So what's happening in your screen shot is that you've set the selected tint color to orange, and that's the end of that. One tab bar item is select and it is tinted orange.

One of the solution is to provide two sets of tab icons. There is a post which is very similar to your situation, you can take a look of it: Custom tab bar icon colors

I think this code (by Tunvir Rahman Tusher) is well explain:

UITabBarItem *tabBarItem1=[[tabBar items] objectAtIndex:0];//first tab bar
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"yourImageSelected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"yourImageUnselected.png"]];//image should be 30 by 30

If you are developing for IOS 10 or newer you can change the unselected tint color, in older versions you can change only the selected tintColor; Here is an implementation:

1) Go to appDelegate / application didFinishLaunchingWithOptions:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    //Check if rootViewController is TabBar
    if (window?.rootViewController as? UITabBarController) != nil {

        //Change unselected TintColor
        (window?.rootViewController as! UITabBarController).tabBar.tintColor = UIColor(red: 255/255, green: 102/255, blue: 0, alpha: 1.0)

        //If system has IOS 10 or newer
        if #available(iOS 10.0, *) {
            //Change Unselected Tint Color
            (window?.rootViewController as! UITabBarController).tabBar.unselectedItemTintColor = UIColor.black
        } else {
            // Fallback on earlier versions
        }

    }

    return true
}

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