0

Is it possible to have an individual tint color for a single UITabBarItem on the tab bar? I only know how to set it universally with [UITabBar appearance] setTintColor:. But, for example, I want one tab to be tinted with blue when selected, the other one with red etc.

I know it can be partially mitigated by setting image and selectedImage properties with UIImageRenderingModeAlwaysOriginal to preserve the original image colors, but the caption text still has the original tint of the entire tab bar.

p4sh4
  • 3,292
  • 1
  • 20
  • 33
  • Please, check my detailed answer on: https://stackoverflow.com/questions/43002013/different-colors-for-tabbar-items-in-tab-bar-controller/47644313#47644313 – erickva Dec 05 '17 at 00:55

4 Answers4

0

Changing the tintColor property individual is impossible, but you can still change the text color for the tab bar item.

For example:

    [theTabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]}
                                   forState:UIControlStateNormal];
    [theTabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor greenColor]}
                                   forState:UIControlStateHighlighted];
Louis Zhu
  • 792
  • 5
  • 12
0

Changing the individual tint color is not impossible, it just requires a fair bit of effort to achieve. The way to do it is to override the tab bar's didSelectItem method. When the user selects an item, you change the tabBar.tintColor to whatever tintColor you want for that item, and then have to call selectedIndex = //The tapped index. I'll post some code if you want, its certainly quite a hack

TimWhiting
  • 2,405
  • 5
  • 21
  • 41
0

Actually, with some tricks, you can set the tint of an individual button.

   let button1 = UITabBarItem(title: "Btn1", image: UIImage(systemName: "heart"), tag: 1)
   let button2 = UITabBarItem(title: "Btn2", image: UIImage(systemName: "heart"), tag: 2)
   let button3 = UITabBarItem(title: "Btn3", image: UIImage(systemName: "heart"), tag: 3)
   let button4 = UITabBarItem(title: "Btn4", image: UIImage(systemName: "heart"), tag: 4)
   let button5 = UITabBarItem(title: "Btn5", image: UIImage(systemName: "heart"), tag: 5)
   tabBar.items = [button1, button2, button3, button4, button5]

   // setting tint of the button4
   if let imageView = tabBar.subviews[4].subviews.first as? UIImageView {
      imageView.tintColor = UIColor.red 
      imageView.image = UIImage(systemName: "heart.fill")
   }

If you decide also to change the image of the button, use the same imageView and not button4.image, otherwise some other random button may change its tint. The tabBar.subviews[0] is the background image to the bar itself, the subsequent subviews are the buttons you added. Each element is of type UITabBarButton that has two subviews: first UITabBarSwappableImageView and the second UITabBarButtonLabel.

igor
  • 691
  • 5
  • 7
-1

For Normal State (unselected):

[tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor grayColor]}
                               forState:UIControlStateNormal];

For Selected State:

[tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blueColor]}
                               forState:UIControlStateHighlighted];
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90