18

I know this question has been asked earlier as well, but still i didn't get any solution searching the solution on internet.

I referred the following posts:

How can I change the text and icon colors for tabBarItems in iOS 7? Only able to change the selected icons color using tintColor.

How to change the color of unselected tab bar items in iOS 7? In this they have written their own GozTabBar class inherited from UIView

I want to changes the default gray color of UITabBar icon when its in unselected state.

Any help would be highly appreciated. Thanks in advance.

Community
  • 1
  • 1
Vinay Jain
  • 2,644
  • 3
  • 26
  • 44
  • You can check this answare for iOS7: http://stackoverflow.com/questions/15086276/uitabbaritem-image-color-is-grey-while-original-image-is-white/22937172#22937172 – Cornel Damian Apr 08 '14 at 12:43

4 Answers4

41

I'm assuming that you do not want to change the color using tintColor? Another option is to use two images that look exactly the same, but differ in color. One image is the selected tab, the other is unselected.

In your AppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions function, try this.

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;

// repeat for every tab, but increment the index each time
UITabBarItem *firstTab = [tabBar.items objectAtIndex:0];

// also repeat for every tab
firstTab.image = [[UIImage imageNamed:@"someImage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
firstTab.selectedImage = [[UIImage imageNamed:@"someImageSelected.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

Edit: For those who don't have the tab bar controller as their root view controller, you can grab the controller like this and the rest of the code is the same.

UITabBarController *tabBarController = self.tabBarController;

codercat
  • 22,873
  • 9
  • 61
  • 85
Inertiatic
  • 1,270
  • 1
  • 9
  • 12
  • it works if your UITabBarController is the rootviewcontroller of your app but it doesn't work for a UITabBarController you add to a UINavigationController (or I am doing something wrong). I will continue to search – phyzalis Apr 25 '14 at 10:06
  • @phyzalis It should work. You have to go up the hierarchy. Get a reference to your nav controller (root), and then your tabbar should be root of that controller. – SmileBot Aug 28 '14 at 15:58
  • @cocoanut you are right and I am sorry I've never comment how I managed that. In fact, I had to do this directly when the UITabBarController is instantiated (in my case, right after [storyboard instantiateViewControllerWithIdentifier:...]). Thanks for your comment – phyzalis Aug 29 '14 at 01:50
  • @cocoanutmobile can you explain how so I go the hierarchy, and how to do next? Thanks – benhi Mar 15 '15 at 09:49
  • Work for me in iOS 9.1 as well – kb920 Jan 27 '16 at 11:58
13

If you already configured the tab bar images with Storyboard, just call this method in ViewDidLoad of your first view:

-(void) configTabBar
{     
    UITabBarController *tabBarController = [self tabBarController];
    UITabBar *tabBar = tabBarController.tabBar;

    for (UITabBarItem  *tab in tabBar.items) {
        tab.image = [tab.image imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
        tab.selectedImage = [tab.image imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
    }
}
nhgrif
  • 61,578
  • 25
  • 134
  • 173
Luismi
  • 1,071
  • 1
  • 13
  • 18
2
[[UITabBar appearance] setTintColor:[UIColor colorWithRed:252/255.0 green:218/255.0 blue:49/255.0 alpha:1.0]];

tabBarItem1.image = [[UIImage imageNamed:@"home_icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    tabBarItem1.selectedImage = [UIImage imageNamed:@"home_icon_selected.png"];

[[UITabBar appearance] setBackgroundColor:[UIColor colorWithRed:15/255.0 green:85/255.0 blue:160/255.0 alpha:1.0]];
    // Change the title color of tab bar items
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIColor whiteColor], NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateNormal];
    UIColor *titleHighlightedColor = [UIColor colorWithRed:252/255.0 green:218/255.0 blue:49/255.0 alpha:1.0];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       titleHighlightedColor, NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateHighlighted]
Muhammad Adnan
  • 2,668
  • 15
  • 27
1

change UIControlStateHighlighted to UIControlStateSelected for iOS8

 [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                           [UIColor whiteColor], NSForegroundColorAttributeName,
                                                           nil] forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                           titleHighlightedColor, NSForegroundColorAttributeName,
                                                           nil] forState:UIControlStateSelected]
posha
  • 881
  • 4
  • 16
  • 28