0

I have this in my UINavigationController:

(void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.

    self.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor greenColor];
    //self.navigationItem.rightBarButtonItem.tintColor = [UIColor greenColor];
    //self.navigationItem.rightBarButtonItem = nextButton;
}

The first two lines work fine: the text of the navigation bar is white and the background is black. The third (and fourth when I tried), just seem to be ignored.

How do I make the text color of the left and right bar buttons be different colors? I see Apple's apps in ios7 doing this.

cdub
  • 24,555
  • 57
  • 174
  • 303

4 Answers4

1

This should work. I just tested with a minimal app. Make sure the leftBarButtonItem is actually set when you try to tint it.

Different tint colors for left and right UINavigationBar items on iOS 7 iPad 3

fzwo
  • 9,842
  • 3
  • 37
  • 57
  • is this ios 7 you are running in? – cdub Oct 02 '13 at 07:48
  • Of course. Oh, I just noticed I only tested on the simulator. I've noticed that especially with regard to bars and tint in iOS 7, behavior and bugs may differ between simulator and device (and even among devices). Be right back. – fzwo Oct 02 '13 at 09:13
  • I've tried it on the device; see attached screenshot. Please forgive the color choice, this was a test app intended to make coloring differences very clear. – fzwo Oct 02 '13 at 09:20
  • Note: If this doesn't work for you, it may be a sequence issue. Maybe your change is being overwritten by something. In my test project (originally created to test something else), I set `rootVC.navigationItem.rightBarButtonItem.tintColor` externally, after its view has been loaded (but before it is displayed). In any case, there is a way to make it work, and if it doesn't work for you right away, try setting the tintColor later (like in loadView). – fzwo Oct 02 '13 at 09:25
  • Is it my eyes or does your picture have blue text on both the left and right sides of the lime green nav bar? – cdub Oct 03 '13 at 00:55
  • Again, excuse the color choice. It's blue on the left, magenta on the right. – fzwo Oct 03 '13 at 05:32
  • Oh okay, then it must be a sequence thing on my end on when the bar and buttons get created. – cdub Oct 03 '13 at 21:28
1

I am not sure but if you want to apply the different colors to left and right bar buttons then just try to apply the tintColor independently and remove the line that sets the tintColor to navigationBar. Just try it.

Update :

It looks like Apple don't want us to apply the different color on left and right bar buttons anymore. Behavior from some of the properties of UINavigationBar has changed from iOS 7. I have already given description in my Answer, you can check it.

Community
  • 1
  • 1
Bhavin
  • 27,155
  • 11
  • 55
  • 94
  • are you running in ios 7, cause the above text for the bar buttons is blue when i run the above code – cdub Oct 02 '13 at 07:47
  • @chris : No. I am just giving you the hint. – Bhavin Oct 02 '13 at 07:49
  • yeah okay, seems ios 7 doesn't like this, the layout transition guide doesn't show this as a possibility - https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TransitionGuide/Bars.html#//apple_ref/doc/uid/TP40013174-CH8-SW1 – cdub Oct 02 '13 at 07:50
  • @chris: Yes, I know that. But I was thinking if they removed that part then they should also remove the `tintColor` property for left and right Bar Buttons. – Bhavin Oct 02 '13 at 07:52
1

I had trouble using the other answers, but this worked great for me.

[self.clearButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                         [UIColor redColor], NSForegroundColorAttributeName,
                                         nil] forState:UIControlStateNormal];

where clearButton is just a reference to my left navigation bar item. Using ios7.

horsejockey
  • 817
  • 10
  • 18
  • 1
    `UITextAttributeTextColor` is deprecated on iOS7, use `NSForegroundColorAttributeName` instead – auco Mar 10 '14 at 16:07
0

I did this by setting a general color in AppDelegate using:

UINavigationBar.appearance().tintColor = tintColor

Then I would create custom bar button items in individual view controllers and set it as either leftBarButton or rightBarButton.

 let filterStr = NSLocalizedString("FILTER", value: "Filter", comment: "Navigation bar item")
 self.searchButton = UIBarButtonItem.init(title: filterStr, style: .plain, target: self, action: #selector(<someMethod()>)     
 self.searchButton?.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.gray], for: UIControlState.normal)
 self.navigationItem.setLeftBarButton(self.searchButton!, animated: false)

enter image description here

Debaprio B
  • 503
  • 7
  • 9