Search code examples
iosobjective-cios7uitabbar

How to change the unselected tabbaritem color in iOS7?


Before iOS 7 I used

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

But now it only paint the selected item, I have read some suggestions but I can not fin how to do it, I used this too:

[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"openbookwp4.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"openbookwp4.png"]];

this put the icon I want, with the color I want, but only after I selected that tab for example, when I open the app the tab looks normal, but after I press the second tab and return to the first, the second tab now has the color I want. It is hard to explain without images, but I can not post images...


Solution

  • This code works on iOS 7.

    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f],
                                                        NSForegroundColorAttributeName : [UIColor colorWithRed:.5 green:.5 blue:.5 alpha:1]
                                                        } forState:UIControlStateNormal];
    

    Set foreground color as you like.

    To affect also the non selected tabbar icons:

    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], UITextAttributeTextColor, nil]
                                             forState:UIControlStateNormal];
    

    If it does not work the only way is with images for selected and unselected states:

    // set selected and unselected icons
    UITabBarItem *item = [self.tabBar.items objectAtIndex:0];
    
    // this way, the icon gets rendered as it is (thus, it needs to be green in this example)
    item.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.tintColor
    item.selectedImage = [UIImage imageNamed:@"selected-icon.png"];