I have a UIToolbar that contains 2 buttons. The toolbar has a tint:
toolbar.tintColor = [UIColor colorWithRed:(102.0/255.0) green:(20.0/255.0) blue:(11.0/255.0) alpha:1];
How can I make the buttons have a similar tint color?
I have a UIToolbar that contains 2 buttons. The toolbar has a tint:
toolbar.tintColor = [UIColor colorWithRed:(102.0/255.0) green:(20.0/255.0) blue:(11.0/255.0) alpha:1];
How can I make the buttons have a similar tint color?
I found this solution preferable to those listed here: Tint UIButton and UIBarButtonItem. Unlike the accepted answer, it allows you to change the color of UIBarButtonItems independent of the UINavigationBar.
In case the link goes down in the future, the gist of it is that you create a tinted UISegmentedControl (with UISegmentedControlStyleBar) with one segment, then create a UIBarButtonItem using that as its custom view.
In iOS 5, UIBarButtonItem has a tintColor
property.
The best thing to do is set the tintColor
AFTER you add buttons to it, as in iOS 4.0, it no longer updates buttons added to the bars after the tintColor
has been set.
Found the answer in this post:
UIToolbar tint on iOS 4
see "Changing colors of UINavigationBarButtons"
EDIT: I remove the link because the domain is down...
The is the text from google cache:
Alright, here’s another quick tip. “How to change the colors of a button on a toolbar.” Of course, this can be applied to any toolbar but I am going to demonstrate the procedure on a UINavigationBar.
The above image only shows a couple of colors. In truth, you can make the button any color that you want. Fantastic! The code is really simple to do this as well. The first thing that we want to do is open the header file for whichever object will be turning a nav bar button a different color and declare the forward class UINavigationButton. You can get this class by either iterating through the subviews of the UINavigationBar, reading its subviews class names, or by class-dumping UIKit if you have a jailbroken device.
Place the following line before your interface declaration:
@class UINavigationButton;
Now, declare a new method in the header that we will use to actually change the button’s color.
- (void)changeNavigationButtonColorToColor:(UIColor *)newColor
Or something similar to the above line of code.
Now, open up your object’s implementation file and implement the above method. Anywhere in your file, add the following method:
- (void)changeNavigationButtonColorToColor:(UIColor *)newColor {
for (UIView *view in self.navigationController.navigationBar.subviews) {
NSLog(@"%@", [[view class] description]);
if ([[[view class] description] isEqualToString:@"UINavigationButton"]) {
[(UINavigationButton *)view setTintColor:newColor];
}
}
}
As you can see above, this is actually a lot easier than it first appears to be. What we first do is set up a for loop to iterate through the subviews of the UINavigationBar using NSFastEnumeration. We then output the class name of the subview, for future reference. IF the class name is UINavigationButton, then we’ve got our view. All we do is set the tintColor property if the UINavigationButton.
That’s it, we’re done!
Alternatively, if you want a wider scope, I’d suggest creating a new UINavigationBar category and placing the button color changing method in there. This was your method can be performed by any class that uses a UINavigationBar without having to recreate the same method over and over.
Remember, a back button and a navigation button are not the same thing. You will have to color the back button separately.
And as usual, here’s a link to a sample app that demonstrates this code: NavButtonColor.zip
UIBarButtonItems
inherently respond to setTintColor
, though it's not public API.
I have a custom UINavigationBar
subclass that runs this block:
for (UIView *subview in self.subviews) {
if ([subview respondsToSelector:@selector(setTintColor:)]) {
[subview performSelector:@selector(setTintColor:) withObject:[UIColor redColor]];
}
}
Obviously replace [UIColor redColor]
with the color of your choosing.
Available in iOS 5.0 and later for UIBarButtonItem:
- (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics
It seems to be the only current solution (IOS 9.0). tintColor is now used for the Image Color.
Swift3
UIBarButtonItem.appearance().tintColor = UIColor.green