5

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?

Guilherme
  • 7,839
  • 9
  • 56
  • 99

3 Answers3

3

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.

pkamb
  • 33,281
  • 23
  • 160
  • 191
1

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)

RdPC
  • 671
  • 10
  • 17
0
[myBarButton_name setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor yellowColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal];
Nimantha
  • 6,405
  • 6
  • 28
  • 69
iPatel
  • 46,010
  • 16
  • 115
  • 137
  • I haven't tested yet, but 1: I believe it will only change text color (not icon color), since it's basic the same thing I did, written in a different way; 2: I'd rather make global changes than a button-by-button basis. – Guilherme Oct 17 '13 at 13:37
  • you mean you want to change color of UIBarButtonItem ? – iPatel Oct 17 '13 at 13:39
  • @Guilherme http://stackoverflow.com/questions/4518617/setting-bar-button-item-color-in-a-custom-navigation-bar and http://stackoverflow.com/questions/13677703/change-uibarbuttonitems-color – iPatel Oct 17 '13 at 13:41
  • No, I want to change the color of the icon/image inside the button. Like this: http://i.stack.imgur.com/AUPfh.png. – Guilherme Oct 17 '13 at 13:42
  • 1
    Best way is create custom button add it.. i tried to find your solution on google .. but can not able to find out :( – iPatel Oct 17 '13 at 13:54
  • @iPatel You do not need a custom button to colorize a standard `UIBarButtonItem`. Just set the button's `tintColor`. – rmaddy Oct 17 '13 at 14:31
  • @rmaddy - but using `tintColor` color.. we can change color of `UIBarButtonItem` not image color.. OP only want to change color of image of built-in `UIBarButtonItem`.. such like http://i.stack.imgur.com/AUPfh.png – iPatel Oct 17 '13 at 14:34
  • @iPatel I know what the OP wants. I do this in my own app. Under iOS 6, in a toolbar, setting the bar button item's `tintColor` only changes the color of the icon, not the whole button. In a navbar it does change the whole button, not the icon. – rmaddy Oct 17 '13 at 14:36
  • @rmaddy - yes.. you are right because i tried (tintColor) with barButton of navbar..so.. – iPatel Oct 17 '13 at 14:38