6

I am trying to change icon color of tab bar item of my custom tab bar,

But setSelectedImageTintColor and setTintColor are not working together.

if that code order is

[[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]];
[[UITabBar appearance] setTintColor:[UIColor redColor]];

then output is

enter image description here

and if that code order is

[[UITabBar appearance] setTintColor:[UIColor redColor]];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]];

then output is

enter image description here

I have used following code inside didFinishLaunchingWithOptions method, first two lines are working fine and the problem is in last two line

//To set color for unselected tab bar item's title color
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary  dictionaryWithObjectsAndKeys: [UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];

//To set color for selected tab bar item's title color
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], NSForegroundColorAttributeName,nil] forState:UIControlStateSelected];


//To set color for unselected icons
[[UITabBar appearance] setTintColor:[UIColor redColor]];

//To set color for selected icons
[[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]];

Note - I have separate custom tabbar class but I am not changing icon color in custom tabbar class

Thanks in anticipation.

Sujay
  • 2,510
  • 2
  • 27
  • 47

2 Answers2

7

First of all, selectedImageTintColoris deprecated as of iOS 8.0.

The only way I have managed to achieve what you want is to have separate images for the selected and unselected state and use UITabBbarItem's selectedImage and image properties respectively.

Important: By default, both these image properties are rendered as "templates", which means they are created from the alpha values in the source image, and thus would get their color from the tabBar's tintColor.

To prevent this, provide images with UIImageRenderingModeAlwaysOriginal.

So, to get what you want, you will need to have two versions of all tabbar-images, one red (for unselected state) and one green (for the selected state) and then do this:

example (swift):

    tabBarItem1.image = UIImage(named:"redHouse")?.imageWithRenderingMode(.AlwaysOriginal)
    tabBarItem1.selectedImage = UIImage(named:"greenHouse")?.imageWithRenderingMode(.AlwaysOriginal)

example (objective-c):

    [tabBarItem1 setImage:[[UIImage imageNamed:@"redHouse"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [tabBarItem2 setSelectedImage:[[UIImage imageNamed:@"greenHouse"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
pIkEL
  • 561
  • 6
  • 14
  • First of all, thanks for your answer. for your information I am using iOS 7 and If I use `UIImageRenderingModeAlwaysOriginal` that will always display original image, but I want to change tab Bar Item's icon color same as tab Bar Item's title color, for selected and unselected both – Sujay Jun 25 '15 at 07:26
  • Yes, but even if you use iOS7, tintColor and selectedImageTintColor is not enough to actually change the icon's color for the **unselected** state. Also note that `tintColor` actually changes BOTH the title and the icon's color for the **selected** state. So the only difference between `tintColor` and `selectedImageTintColor` is that the second one does not change the title color (of a **selected** tabbarItem) – pIkEL Jun 25 '15 at 07:38
  • I edited my original answer so maybe its more clear now what I mean? – pIkEL Jun 25 '15 at 07:46
  • It is simply **not possible** to do this with tint color in iOS7, the question you linked gives the same answer. – pIkEL Jun 25 '15 at 08:08
  • @pIkEL Where in our program do we need to write this code? – G.Abhisek Dec 07 '15 at 09:27
  • @G.Abhisek You should put it wherever you can access your tabBar. The `UITabBar` has a property `var items: [UITabBarItem]?` that gives you the list of items. – pIkEL Dec 09 '15 at 09:39
0

Apple documentation says you can use the tintColor property instead.

tiw
  • 535
  • 1
  • 6
  • 22