oyed
oyed

Reputation: 570

Objective-C - Changing Inactive UITabBar Image Colour

I'm trying to find a way to change the tint colour for inactive images on a UITabBar - here is an image of my current progress enter image description here

I'm trying to change the colour of the gray image to, currently, any other colour, without any luck. Here is the code I am using:

[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
[[UITabBar appearance] setTintColor:[UIColor whiteColor]];

I've been searching on Google for some way to make this work but am having no luck. Any help is greatly appreciated.

Upvotes: 2

Views: 1116

Answers (3)

Thompson
Thompson

Reputation: 1098

I believe it's a two-step process:

1) Create image with UIImageRenderingModeAlwaysTemplate

tabBarItem.image = [tabImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

2) Subclass UIViewController and contribute to -(void)tintColorDidChange

- (void) tintColorDidChange {

  switch (self.tintAdjustmentMode) {

    case UIViewTintAdjustmentModeNormal: {
        tabBarItem.tintColor = [UIColor cyanColor];
    }
      break;

    case UIViewTintAdjustmentModeDimmed: {
        tabBarItem.tintColor = [UIColor magentaColor];
    }
      break;

    case UIViewTintAdjustmentModeAutomatic: {
        tabBarItem.tintColor = [UIColor yellowColor];
    }
      break;
  }
}

-(void)tintColorDidChange will fire whenever the tintColor is changed by the system.

Upvotes: 0

RobP
RobP

Reputation: 9522

You can do this one at a time for each tab bar button as follows:

UIImage *rawImage = [[myVC tabBarItem] image];
[[myVC tabBarItem] setFinishedSelectedImage:[rawImage imageWithColorMask:[UIColor grayColor]] withFinishedUnselectedImage:[rawImage imageWithColorMask:[UIColor greenColor]]];

EDIT: Above is the best choice if you want to support iOS before iOS 7, but this method is deprecated. For iOS 7 and above you can just set your tab bar item's image to an image which specifies that you don't want iOS to do color masking for you, as follows:

// assuming you have a UIImage ready to go...
myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[[myVC tabBarItem] setSelectedImage:myImage];

Upvotes: 1

Sam
Sam

Reputation: 2579

If your are trying to achieve displaying of the actual image at the UITabBar then use the following code:

tabBarItem.image = [tabImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

and if you want to display image in original condition for the selected then use the following :

tabBarItem.selectedImage = [tabImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

these two are alternative to the deprecated methods setFinishedSelectedImage: and withFinishedUnselectedImage:

Upvotes: 2

Related Questions