13

How can i specify the tint of images when a tab is selected and unselected?

I have tried this but it doesnt work:

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

This makes the selected image tint red(not green) and unselected tint gray (not red).

0xSina
  • 20,973
  • 34
  • 136
  • 253

4 Answers4

25

You can set the tint color for selected and unselected tab bar buttons like this:

[[UIView appearanceWhenContainedIn:[UITabBar class], nil] setTintColor:[UIColor redColor]];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]];

The first line sets the unselected color - red in this example - by setting the UIView's tintColor when it's contained in a tab bar. Note that this only sets the unselected image's tint color - it doesn't change the color of the text below it.

The second line sets the tab bar's selected image tint color to green.

Josh Brown
  • 52,385
  • 10
  • 54
  • 80
  • 8
    In iOS 8, at least, using the code above, the unselected image initially shows as red. However, the label does not, and once the selection changes, the unselected color reverts to its default. – Fostah Sep 30 '14 at 20:14
  • 3
    For me it changes back to gray after i select the tab and then deselect it...?!? Strange thing.. I am doing nothing but those appearance proxy things... – Georg Jan 26 '15 at 19:53
  • For swift (post ios 8) you can use: UITabBar.appearance().tintColor = UIColor. – zevij Nov 09 '15 at 12:05
0

Are you using the template-version of your images?

Instead of setting your images with [UIImage imageNamed: @"MyImage"], set them with [[UIImage imageNamed: @"MyImage"] imageWithRenderingMode: UIImageRenderingModeAlwaysTemplate].

This setup along with your code should solve this issue.

G33kz0r
  • 750
  • 1
  • 8
  • 17
  • 1
    This doesn't actually change the tint color of unselected tab bar item images. They are gray no matter what. Can you confirm that this will indeed change the tint of unselected item to whatever you specify? – 0xSina Oct 08 '13 at 03:08
0

You have to use the new Image rendering modes introduced in iOS 7 (UIImageRenderingModeAlwaysOriginal and UIImageRenderingModeAlwaysTemplate )see my answer to a similar question:

Hope this helps

Community
  • 1
  • 1
gav
  • 1,293
  • 13
  • 14
-1

if you do not have many viewcontrollers. Here is my way to do it.

In your delegate method just place your tabbar bg Image. And set the UIImageView

Create UITabbar intance in AppDelegate.h

@property (nonatomic,retain) UITabBar *tabbar;

And

@synthesize tabbar;

UITabBarController *tabBarController =
    (UITabBarController *)self.window.rootViewController;
tabbar = [tabBarController tabBar];

[tabbar setBackgroundImage:[UIImage imageNamed:@"tabbarBg.png"]];
 NSArray *tabImageArray  = [NSArray arrayWithObjects:
                                [UIImage imageNamed:@"tab1Hover.png"],
                                [UIImage imageNamed:@"tab2.png"],
                                [UIImage imageNamed:@"tab3.png"],
                                [UIImage imageNamed:@"tab4.png"],
                                [UIImage imageNamed:@"tab5.png"],
                               nil];

    for (int i = 0; i<5; i++) {
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(20+i*60+i*3.5, 10, 25, 21)];
        [image setContentMode:UIViewContentModeScaleAspectFit];
        [image setImage:[tabImageArray objectAtIndex:i]];
        [image setTag:10+i];
        [tabbar addSubview:image];
    } 

Then every ViewController in tabbar add

-(void)viewWillAppear:(BOOL)animated 

delegate method and in this method. You can change the Imageviews as shown below.

-(void)viewWillAppear:(BOOL)animated{
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
UITabBarController *tabBarController = (UITabBarController *)appDelegate.window.rootViewController;
 NSArray *tabImageArray  = [NSArray arrayWithObjects:
                               [UIImage imageNamed:@"tab1Hover.png"],
                               [UIImage imageNamed:@"tab2.png"],
                               [UIImage imageNamed:@"tab3.png"],
                               [UIImage imageNamed:@"tab4.png"],
                               [UIImage imageNamed:@"tab5.png"],
                               nil];
for (int i = 0; i<5; i++) {
        UIImageView *image = (UIImageView*)[tabbar viewWithTag:10+i];
        [image setImage:[tabImageArray objectAtIndex:i]];
    }
}

So, just costumize tabImageArray in every View controller. Then you can use it.

I works on iOS 7 as well.

mialkan
  • 342
  • 3
  • 7