JayVDiyk JayVDiyk - 7 months ago 91
Swift Question

Change Default "Not Selected" UITabBarItem Image Color

<--- Note: Please help vote UP this question if you don't mind

how do we change the "Not Selected" or the unhiglighted state of the icons in UITabBarItem?

I tried setting the UITabBarItem.appearance().setTitleTextAttributes(:) but it only changes the text color.

Any idea?

enter image description here

Answer

If you want to change the default in iOS 7 and above, you have to actually use different icons (in the color you like to have for unselected tabs) and set the color of the text. Instead of creating two sets of icons, you could apply this tweak:

// set the selected colors
[self.tabBar setTintColor:[UIColor whiteColor]];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];


UIColor * unselectedColor = [UIColor colorWithRed:184/255.0f green:224/255.0f blue:242/255.0f alpha:1.0f];

// set color of unselected text
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:unselectedColor, NSForegroundColorAttributeName, nil]
                                     forState:UIControlStateNormal];

// generate a tinted unselected image based on image passed via the storyboard
for(UITabBarItem *item in self.tabBar.items) {
   // use the UIImage category code for the imageWithColor: method
   item.image = [[item.selectedImage imageWithColor:unselectedColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

Source.

Comments