Benoit Thierry
Benoit Thierry

Reputation: 13

UIBarButtonItem override custom image color - Objective-c

I'm having a huge problem with my UIBarButtonItem. I'd like to use a custom image depending on the torch state (on/off) but it's overriding my beautiful yellow icon to a blue "tasteless" icon.

Any idea or link that I missed about that?

Upvotes: 1

Views: 244

Answers (2)

simbesi.com
simbesi.com

Reputation: 1529

From Apple's documentation refer below method and it's available from iOS 7.0 and later:

- (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode

To make compatible with below iOS 7.0 You have to check during the runtime this method is available or not.

UIImage *yourImage = [UIImage imageNamed:@"yourButifulImage"];
if ([yourImage respondsToSelector:@selector(isAvailabeImageWithRenderingMode:)]) 
{
   tabBarItem.image = [yourImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
} 
else 
{
   tabBarItem.image = yourImage;
}

Hope this will solve your problem.

Upvotes: 2

Nilesh Jha
Nilesh Jha

Reputation: 1636

UIImage *myImage = [UIImage imageNamed:@"OnImage"];
myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithImage:myImage style:UIBarButtonItemStylePlain target:nil action:nil];

Depending on your condition you can change the image.

Upvotes: 1

Related Questions