0

Is there an easy way to change the text color of a UIBarButtonItem without using an UIImage as the background?

By default the text is always white. I'd like to make it black.

barfoon
  • 27,481
  • 26
  • 92
  • 138
skålfyfan
  • 4,931
  • 5
  • 41
  • 59

4 Answers4

6

Thank to IOS 5.0 you do not need to use images for that.You can set various text attributes with following code.

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"Title"     style:UIBarButtonItemStyleBordered target:nil action:nil];

if ([button respondsToSelector:@selector(setTitleTextAttributes:forState:)]) {

    NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                               kTextColor,UITextAttributeTextColor,
                                               nil];

    [[UIBarButtonItem appearance] setTitleTextAttributes:textAttributes                   
                                  forState:UIControlStateNormal];

}
Ilker Baltaci
  • 11,644
  • 6
  • 63
  • 79
1

I'd rather use a UIButton inside a UIBarButtonItem and customize that one.

This is an example with custom graphics for a custom UIButton. The idea stays the same use initWithCustomView of the UIBarButtonItem to put something else in it which is easily customizable.

self.closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[closeButton setImage:[UIImage imageNamed:@"webview_close_button_normal.png"] forState:UIControlStateNormal];
[closeButton setImage:[UIImage imageNamed:@"webview_close_button_pressed.png"] forState:UIControlStateHighlighted];
closeButton.frame = CGRectMake(0, 0, 121, 36);
[closeButton addTarget:self action:@selector(closeAdViewController) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem * aBarButtonAdClose = [[[UIBarButtonItem alloc] initWithCustomView:closeButton] autorelease];
Nick Weaver
  • 47,228
  • 12
  • 98
  • 108
1

You can set a custom view by using

[[UIBarButtonItem alloc] - (id)initWithCustomView:(UIView *)customView]

picciano
  • 22,341
  • 9
  • 69
  • 82
0

The direct answer to your question is no.

You can't simply change the color of a UIBarButtonItem by setting one of its properties. You can set the tintColor property of the UIToolBar or UINavigationBar that contains the UIBarButtonItems, but that also affects the color of the toolbar itself and doesn't offer too much customization.

If that doesn't work for you, a custom view as the other answers suggest is a fine idea.

Good luck!

CharlieMezak
  • 5,999
  • 1
  • 38
  • 54
  • 2
    Just to note, the tintColor property is not supported pre iOS 5. – barfoon Feb 09 '12 at 21:14
  • Actually, according to Apple's docs, tintColor has been available since iOS 2.0 – CharlieMezak Feb 10 '12 at 21:46
  • 2
    According to Apple's docs, not for `UIBarButtonItem` - https://developer.apple.com/library/IOs/documentation/UIKit/Reference/UIBarButtonItem_Class/Reference/Reference.html#//apple_ref/occ/instp/UIBarButtonItem/tintColor – barfoon Feb 10 '12 at 22:01
  • That's not what I was suggesting in my answer, but you're right about that. – CharlieMezak Feb 12 '12 at 01:15