Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change tintColor of unselected UITabBarController item title and background image

How can I change the tintColor of an unselected UITabBarItem title and background image iOS 8?

The default color for an unselected state is a light gray color, but it does not show on my darkish shade UITabBar background

I'd like my unselected state to have a color of [UIColor blackColor]

Inside my app delegate didfinishlaunchingwithoptions: I have

UIImage *deselectedE = [[UIImage imageNamed:@"mincraft_axe_green_32.png"] imageWithRenderingMode:UIImageRenderingModeAutomatic];
UIImage *selectedE = [[UIImage imageNamed:@"mincraft_axe_green_32.png"] imageWithRenderingMode:UIImageRenderingModeAutomatic];
e.tabBarItem =  [[UITabBarItem alloc] initWithTitle:@"Profile" image:deselectedE selectedImage:selectedE];
[[UITabBar appearance] setTintColor:[UIColor blackColor]];
like image 831
SleepsOnNewspapers Avatar asked Oct 24 '14 15:10

SleepsOnNewspapers


2 Answers

Figured it out!

Use this to change the color of the text:

[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor greenColor] }
                                         forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor] }
                                         forState:UIControlStateSelected];

And make sure that image rendering mode is set to ORIGINAL for the images

UIImage *deselectedImage = [[UIImage imageNamed:@"deselectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *selectedImage = [[UIImage imageNamed:@"selectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
like image 187
SleepsOnNewspapers Avatar answered Sep 18 '22 17:09

SleepsOnNewspapers


In your AppDelegate.m inside of application didFinishLaunchingWithOptions: use the following code:

//unselected icon tint color 
[[UIView appearanceWhenContainedIn:[UITabBar class], nil] setTintColor:[UIColor redColor]];

//selected tint color 
[[UITabBar appearance] setTintColor:[UIColor greenColor]];

//text tint color 
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] }
                                     forState:UIControlStateNormal];

//background tint color 
[[UITabBar appearance] setBarTintColor:[UIColor blueColor]];
like image 32
TheSD Avatar answered Sep 17 '22 17:09

TheSD