13
votes

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).

4
I dont think you can tint the 'unselected' state, its always gray.KDaker

4 Answers

25
votes

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.

0
votes

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.

0
votes

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

-1
votes

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.