1

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.

enter image description here

Image of the tab bar

enter image description here

How do I change the image color?

Community
  • 1
  • 1
s.d.
  • 363
  • 3
  • 14

3 Answers3

0

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • You are right. Thats what happened. Now how do I correct it? – s.d. Oct 16 '15 at 19:44
  • You can't use a tinted image. You will have to use a non-tinted `image` and a non-tinted `selectedImage` and now the colors (and transparency) of those images will be used directly. – matt Oct 16 '15 at 20:51
  • 1
    as of ios 10+, you can do it programmatically with: `tabBarCtrl.tabBar.unselectedItemTintColor = UIColor.gray` in your app delegate – Anchor Oct 29 '16 at 05:34
0

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
Community
  • 1
  • 1
CTCoder
  • 23
  • 7
0

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
}
reojased
  • 709
  • 1
  • 7
  • 19