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
and if that code order is
[[UITabBar appearance] setTintColor:[UIColor redColor]];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]];
then output is
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.
First of all, selectedImageTintColor
is 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]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With