2

I created a custom navigation bar and a right navigation button using the XIB. This works fine. But I need to to customize the tint color of the right navigation button. At the moment this tint color is the same color as tint color of navigation bar. I need a different color for this right button. Is there any way to change this color?

Thank you.

Dilshan
  • 3,231
  • 4
  • 39
  • 50

4 Answers4

14

In iOS 5 there is 'appearance' feature:

[[UIBarButtonItem appearance] setTintColor:YOUR_COLOR];

pbibergal
  • 2,901
  • 1
  • 17
  • 19
4

Not to my knowledge, the button inherits the tint color of the navigationBar. What you can do is set a customView for the navigationItem:

This is how you set it with one of the SDK's buttons:

UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareButtonHandler:)];

[self.navigationItem setRightBarButtonItem:shareButton];
[shareButton release];

Instead you can do it like this:

UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"yourImage.png" style:UIBarButtonItemStyleBordered target:self action:@selector(customButtonHandler:)]];

To use an image you made in photoshop etc.

There is als an initWithCustomView:UIView or initWithTitle:NSString you can use.

Sorry no "one-line solution" :)

Krease
  • 15,805
  • 8
  • 54
  • 86
RickiG
  • 11,380
  • 13
  • 81
  • 120
1

Swift 3.x

UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : Color.primaryActionColor], for: .normal)
Ilker Baltaci
  • 11,644
  • 6
  • 63
  • 79
  • The problem here is that every UIBarButtonItem is going to have that color. To avoid this, you can do : UIBarButtonItem.appearance(whenContainedInInstancesOf: [UIToolbar.self]).setTitleTextAttributes(...) – Hibbem Jan 25 '18 at 10:51
0

If you have navigation bar button item with custom view you should convert to UIButton first:

navigationItem.rightBarButtonItem?.customView as? UIButton

Then you can add the attributes of UIButton such as:

button.setTitleColor(UIColor.black.withAlphaComponent(0.1), for: .normal)
erdikanik
  • 674
  • 9
  • 11