Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserving the original image color of the selected and unselected UITabBar icons

Tags:

The structure is the following:

In my storyboard, I have a Tab Bar Controller which contains a Tab Bar object. This object has a custom class in which I have only this method:

- (void)awakeFromNib
{
  NSArray *imageNames = @[@"test1", @"test2", @"test3", @"test4", @"test5"];
    for (int i=0; i<5; i++) {
      UITabBarItem *item = [self.items objectAtIndex:i];
      NSString *imageName = [imageNames objectAtIndex:i];
      item.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
      item.selectedImage = [UIImage imageNamed:[imageName stringByAppendingString:@"-selected"]];
    }
}

I have created an Asset Catalog which contains my tab bar icons, both the selected and unselected versions (different colors).

The documentation for UIImageRenderingModeAlwaysOriginal mentions Always draw the original image, without treating it as a template. which should preserve the original image color. This doesn't seem to happen and this suggestion didn't work either.

What happens instead is that in the selected state, my tab bar icons get the default blue tint color.

One thing that I noticed is that if I do the following (taken from here) in the didFinishLaunchingWithOptions in the AppDelegate, then I can set the color of the selected tab bar icons as I want:

[[UITabBar appearance] setTintColor:[UIColor purpleColor]];

The documentation for the setTintColor mentions:

The tint color to apply to the tab bar’s tab bar items. Starting in iOS 7.0, the tint color that applies to a tab bar’s tab bar items is the first nondefault tint color in the view hierarchy, starting with the tab bar itself.

Does this mean that regardless of my tab bar icons's color (original in the images themselves), in the selected state they will always have the color of tintColor ?

Or am I doing something wrong/missing something?

I'm having this issue on iOS 7.0 and 7.1.

like image 665
Alex Avatar asked Apr 03 '14 19:04

Alex


2 Answers

Just go to your image assets properties, and set Render as property to "original image"

enter image description here

And you're good to go!

like image 198
Abdoelrhman Avatar answered Nov 02 '22 23:11

Abdoelrhman


Perfect question, really well explained.

You are not setting imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal for the selected image.

Just should add imageWithRenderingMode:UIImageRenderingModeAlwaysOriginalto the selectedImage:

  item.selectedImage = [[UIImage imageNamed:[imageName stringByAppendingString:@"-selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

Check this other answer.

like image 42
Gabriel.Massana Avatar answered Nov 03 '22 01:11

Gabriel.Massana