Unselected UITabBar color? Unselected UITabBar color? ios ios

Unselected UITabBar color?


In iOS 10 and higher, there are 3 possible easy solutions:

A. Instance from code (Swift):

self.tabBar.unselectedItemTintColor = unselectedcolor

B. Instance from IB:

Add a Key Path: unselectedItemTintColor of type: Color

C. Global appearance (Swift):

UITabBar.appearance().unselectedItemTintColor = unselectedcolor


This will not work under iOS 7 as far as I can say. In particular, tintColor of the tab bar will define the color of the selected tab, not of the unselected ones. If you want to change the default in iOS 7, it seems that you have to actually use different icons (in the color you like to have for unselected tabs) and set the color of the text.

This example should tint selected tabs to red and render others in green. Run this code in your TabBarController:

// set color of selected icons and text to redself.tabBar.tintColor = [UIColor redColor];[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];// set color of unselected text to green[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], NSForegroundColorAttributeName, nil]                                         forState:UIControlStateNormal];// set selected and unselected iconsUITabBarItem *item0 = [self.tabBar.items objectAtIndex:0];// this way, the icon gets rendered as it is (thus, it needs to be green in this example)item0.image = [[UIImage imageNamed:@"unselected-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];// this icon is used for selected tab and it will get tinted as defined in self.tabBar.tintColoritem0.selectedImage = [UIImage imageNamed:@"selected-icon.png"];

If you set the icon in the story board only, you can control the color of the selected tab only (tintColor). All other icons and corresponding text will be drawn in gray.

Maybe someone knows an easier way to adopt the colors under iOS 7?


Extending @Sven Tiffe’s answer for iOS 7, you can get your code to automatically tint the unselected UITabBar images added in the storyboard. The following approach will save you having to create two sets of icon images (i.e. selected vs unselected) and having to programatically load them in. Add the category method imageWithColor: (see - How can I change image tintColor in iOS and WatchKit) to your project then put the following in your custom UITabBarController viewDidLoad method:

// 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 storyboardfor(UITabBarItem *item in self.tabBar.items) {    // use the UIImage category code for the imageWithColor: method    item.image = [[item.selectedImage imageWithColor:unselectedColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];}

Create a Category called UIImage+Overlay and on UIImage+Overlay.m (extracted from this answer ) :

@implementation UIImage(Overlay)- (UIImage *)imageWithColor:(UIColor *)color1{        UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);        CGContextRef context = UIGraphicsGetCurrentContext();        CGContextTranslateCTM(context, 0, self.size.height);        CGContextScaleCTM(context, 1.0, -1.0);        CGContextSetBlendMode(context, kCGBlendModeNormal);        CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);        CGContextClipToMask(context, rect, self.CGImage);        [color1 setFill];        CGContextFillRect(context, rect);        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();        UIGraphicsEndImageContext();        return newImage;}@end