Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 6, How to tint UIBarButtonItem white icons?

I have UIBarButtonItems in my app with a light color background, and I wanted to tint the icons with a darker color, instead of the standard white.

Like this:

enter image description here

I was able to tint text using

[[UIBarButtonItem appearance] setTitleTextAttributes:@{UITextAttributeTextColor: [UIColor blackColor]} forState:UIControlStateNormal];,

but I can't figure out how to change the icon colors.

Is it possible to do that?

like image 975
Guilherme Avatar asked Oct 03 '22 16:10

Guilherme


2 Answers

Create a UIButton with your custom image, then create a UIBarButtonItem with the UIButton as a custom view:

UIImage *buttonImage = [UIImage imageNamed:@"image"]; // Tint this image programmatically
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *barButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];

Note that this is mainly for iOS 6 and below. In iOS 7+ you get this behavior for free with tintColor.

like image 135
pkamb Avatar answered Oct 13 '22 10:10

pkamb


I believe this might work in the navigation bar:

[[UINavigationBar appearance] setTintColor:[UIColor blackColor]];

[[UIBarButtonItem appearance] setTintColor:[UIColor blackColor]];

And of course, you can also change the whole tint in the storyboard options (not on my mac, so can't tell you exactly where)

like image 37
RdPC Avatar answered Oct 13 '22 11:10

RdPC