James Frost
James Frost

Reputation: 6990

How to set a different unselected image and text color on UITabBarItem

I'd like to set different colors for a UITabBarItem's title text and image in the unselected state.

For the selected state, I can accomplish this like so:

[[UITabBar appearance] setTintColor:[UIColor purpleColor]]; // image color
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName: [UIColor orangeColor] } forState:UIControlStateSelected]; // text color

For the unselected state, I'm attempting the following:

[[UITabBar appearance] setUnselectedItemTintColor:[UIColor blueColor]]; // image color
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName: [UIColor redColor] } forState:UIControlStateNormal]; // text color

But for some reason, the unselectedItemTintColor setting overrides whatever I try and set for the titleTextAttributes – so in the snippet above, both the text and image would appear blue.

I've also tried changing the titleTextAttributes directly on the UITabBarItem after I've created it (instead of using appearance), but again this seems to have no effect.

How can I achieve different unselected colors? Is it possible?

Upvotes: 1

Views: 329

Answers (1)

James Frost
James Frost

Reputation: 6990

I managed to solve this shortly after posting. It turns out that while setting the unselectedItemTintColor using UIAppearance overrides the titleTextAttributes for the item, everything works correctly if you set the unselectedItemTintColor directly on the tab bar itself.

So instead of

[[UITabBar appearance] setUnselectedItemTintColor:[UIColor blueColor]];

just do

[self.myTabBarInstance setUnselectedItemTintColor:[UIColor blueColor]];

Upvotes: 2

Related Questions