How to change the color of a UIBarButtonItem

EDIT: There is a new way to do this in iOS 5 that is much easier and nicer. If you want to support iOS 4, keep reading.

A UIBarButtonItem is an item that goes on a UIToolbar or a UINavigationBar. It’s typically a button but it can be a bunch of different things. I’m going to talk about the button. A UIBarButtonItem button takes on the color of the bar that it is on. You can set the tint of a UIToolbar, but you can’t change the colors of the buttons on it.

I found lots of different solutions for this online, most involving adding a UIButton as the customView of the UIBarButtonItem and having some sort of image for the color you need. That works fine, until you need a color that you don’t have an image for.

I found a better solution here. Basically what they figured out was that a UISegmentedControl with a style of UISegmentedControlStyleBar looks just like a UIBarButtonItem. A key difference is that UISegmentedControl has a nice tintColor property that you can use to change the colors.

So what I did was take CharlyBr‘s code and put it into a UIBarButtonItem category.

+(UIBarButtonItem*)barButtonItemWithTint:(UIColor*)color andTitle:(NSString*)itemTitle andTarget:(id)theTarget andSelector:(SEL)selector
{
	UISegmentedControl *button = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:itemTitle, nil]] autorelease];
	button.momentary = YES;
	button.segmentedControlStyle = UISegmentedControlStyleBar;
	button.tintColor = color;
	[button addTarget:theTarget action:selector forControlEvents:UIControlEventValueChanged];

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

	return removeButton;
}

You can check out my example project on GitHub.

This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

5 Responses to How to change the color of a UIBarButtonItem

  1. Dan says:

    Looks good, except for a couple minor issues.

    1. Any workaround for the fact that the action fires on touch down, not touch up?

    2. On iOS 3.1.3, I’m not seeing the tint. It works on 4.0. I’m basically just doing self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithTint...] in viewDidLoad. Any suggestions?

    Thanks!

  2. Randall says:

    1. I tried changing the control event to something different from UIControlEventValueChanged but I couldn’t get the selector to fire. You might have better luck than me.

    2. I’m not sure why it wouldn’t work in iOS 3. Unfortunately I don’t have the SDK installed or any devices to check.

  3. Pingback: How to change the color of UIBarButtonItem on iOS 5 | farp.blog

  4. Simon says:

    Confirm, does not tint on 3.x

  5. Pingback: navigationItemのUIBarButtonItemを好きな色にする « Code? – Design?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>