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:
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?
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
.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With