Is it possible to have a red UIBarButtonItem?
11 Answers
If anyone is looking for code to exactly duplicate a simple UIBarButtonItem:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];
[button setTitle:@"Delete" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0f];
[button.layer setCornerRadius:4.0f];
[button.layer setMasksToBounds:YES];
[button.layer setBorderWidth:1.0f];
[button.layer setBorderColor: [[UIColor grayColor] CGColor]];
button.frame=CGRectMake(0.0, 100.0, 60.0, 30.0);
[button addTarget:self action:@selector(batchDelete) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* deleteItem = [[UIBarButtonItem alloc] initWithCustomView:button];
And delete.png is:

- 1,780
- 1
- 16
- 17
-
8One additional note : you need to add QuartzCore library to your project and import it in your class to use the CALayer methods. – Johanisma Jul 11 '11 at 14:49
-
6To save others having to look it up: #import
– Dan J Nov 22 '11 at 23:15 -
2I think the default corner radius might be 5.0f instead of 4.0f. Also, people may also prefer to use a 1 pixel wide image to keep the image small. This post was useful though, thanks! – Dan J Nov 23 '11 at 21:23
-
do you happen to have this blue image? – newton_guima Jun 12 '12 at 18:38
-
Instead of adding a UIButton to a UIBarButtonItem you should just create a custom UIView and add that to the UIBarButtonItem. – Brody Robertson Sep 04 '12 at 20:28
-
@bdrobert but what about the addTarget - how do you press the UIView if it's not a button? Or should that be done on the UIBarButtonItem that wraps it? – Rhubarb Nov 24 '12 at 17:53
Why not just :
[[self editButtonItem] setTintColor:[UIColor redColor]];
(on sdk 4.* , ios 5 and up)

- 4,764
- 2
- 50
- 72

- 664
- 6
- 9
You can create custom UIButton with your desired look and initialize your UIBarButtonItem with it as a custom view.
UIButton *button = [UIButton buttonWithType:....];
...(customize your button)...
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];

- 356
- 3
- 4
-
1But this does not give you a UIBarButtonItem that matches iOS's default rendering, unless you're willing to painstakingly replicate iOS default rendering behavior as Scott does. If, however, you want to depart from the default look then this is clearly the way to go. – charshep Aug 02 '12 at 23:19
You can set the tintColor property of a UIBarButtonItem in iOS 5. If you need to support iOS 4, check out this blog post. It details using a UISegmentedControl styled to look like a single button.

- 14,691
- 7
- 40
- 60
Using a custom image doesn't work as it just uses the alpha to render the button.

- 2,468
- 1
- 25
- 40
Okay, there is actually a much better solution to this, in my opinion, which involves less code and uses the native UI controls.
The trick is to use a UISegmentedControl, and remove all the segments except one. (Can't be done in IB, has to be done programmatically).
Once you've done that, you set the tint colour, then create a UIBarButtonItem passing it the UISegmentedControl as a custom view.
This guy's used the technique to create a category of UIBarButtonItem which works brilliantly:
http://fredandrandall.com/blog/2011/03/31/how-to-change-the-color-of-a-uibarbuttonitem/

- 3,928
- 2
- 34
- 50
-
Great suggestion if you have to support 4.3 as I do. I'm going with the category as detailed in the link. – David H Feb 23 '12 at 22:22
If its the UIBarButtonItem of a UIToolbar
set this before your implementation in the file you want this code
@class UIToolbarTextButton;
Then change the toolbar buttons doing this:
for (UIView *view in self.navigationController.toolbar.subviews) {
NSLog(@"%@", [[view class] description]);
if ([[[view class] description] isEqualToString:@"UIToolbarTextButton"]) {
[(UIToolbarTextButton *)view setTintColor:yourcolor];
}
}

- 921
- 8
- 18
You can create a custom image for the button and use that. Else, if you have set the tintColor of your navbar or toolbar to red, the button items on these will also appear red.

- 70,519
- 61
- 198
- 274
When you have UIBarButtonItem setup in a Storyboard, you have to set the tintColor in the viewWillAppear()
or viewDidAppear()
method (putting it in viewDidLoad() doesn't work for me...):
- (void)viewWillAppear {
[super viewWillAppear];
[[self editButtonItem] setTintColor:[UIColor redColor]];
}
Or in Swift:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
editButtonItem.tintColor = UIColor.redColor()
}
Note: tested on iOS 8/9.

- 1,042
- 10
- 7
If using IB (Interface Builder), drag a UIView
from the Library onto the UIToolBar
, and it will generate a UIBarButtonItem
with a custom view. You can then add any other controls you want to it, e.g. UIButton
, UILabel
, UIActivityIndicatorView
.

- 4,983
- 5
- 27
- 29
I wanted to add something to the highlighted solution. If you do this subclassing, it will not execute storyboard-defied segues and selectors that are assigned to the UIBarButtonItem. Further, the 'target' and 'action' properties are not set in the UIBarButtonItem so you can't access them from the subclass and assign them to your UIButton.
I worked around this by adding 2 properties to my subclass (using ARC):
@property (nonatomic, weak) id targetViewController;
@property (nonatomic, strong) NSString *segueIdentifier;
Next, in the init of your subclass, add something like this to the accepted answer code:
[button addTarget:self action:@selector(performAction) forControlEvents:UIControlEventTouchUpInside];
and a function:
- (void)performAction {
if (_targetViewController && _segueIdentifier) {
[_targetViewController performSegueWithIdentifier:_segueIdentifier sender:self];
} else {
NSLog(@"Requires targetViewController and segueIdentifier to be set");
}
}
finally, in your main view controller that is hosting all of this, in the 'viewDidLoad', add this:
_fancyBarButtonItem.segueIdentifier = @"NewTransaction";
_fancyBarButtonItem.targetViewController = self;
Hope this helps someone else save time when they start down this path with Storyboards/iOS5

- 656
- 9
- 19