3

In iOS6, I used this code to make my UIBarButtonItem:

UIBarButtonItem* validate = [[UIBarButtonItem alloc]initWithTitle:@"MyTitle" style:UIBarButtonItemStylePlain target:self action:@selector(actionValidate)];
    [validate setTintColor:[UIColor orangeColor]];
    self.navigationItem.rightBarButtonItem = validate;

It works fine in iOS6 but in iOS7, the color of the button changes only when you push it.. How can I fix this?

suztomo
  • 5,114
  • 2
  • 20
  • 21
Anthony
  • 2,801
  • 3
  • 30
  • 49

2 Answers2

5

In iOS7 you if you need to change the navigationBar buttons color, you must set tintColor for the navgationBar not the for specific barButton any more.

navigationController.navigationBar.tintColor = [UIColor orangeColor];

Edit: this works in iOS7, you need to do the check:

float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (systemVersion >= 7.0)
{
    navigationController.navigationBar.tintColor = [UIColor orangeColor]
}
Chris
  • 5,485
  • 15
  • 68
  • 130
Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68
  • I do not want to change the entire bar, only the button. I use the IOS6 interface even if the user is in IOS 7. – Anthony Oct 02 '13 at 07:29
  • It changes the bar buttons NOT the bar itself. – Tarek Hallak Oct 02 '13 at 07:37
  • What if you only want to change one of the buttons and not all the buttons? – stevex Nov 15 '13 at 21:23
  • @stevex Then you need to fake this button by using a custom view. – Tarek Hallak Nov 15 '13 at 22:09
  • 1
    I highly recommend not comparing versions with a float - that will not compare minor versions properly (ie 7.11 vs 7.2), and it will clobber anything after the minor version number (7.1.1). Take a look at [this question](http://stackoverflow.com/questions/3339722/how-to-check-ios-version) for some options. – thegrinner Apr 16 '14 at 18:06
2

I couldn't get @Chris's method to work (iOS 8 like @Adama says).

My use case is that I want to set all UIToolbar & UINavigationBar buttons to a default colour. So using the UIAppearance API:

UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.redColor()], forState: .Normal)

toriaezunama
  • 311
  • 2
  • 11