Change the color of a bar button in iOS 7

cdub picture cdub · Sep 30, 2013 · Viewed 11k times · Source

I have the following code in my viewDidLoad of my containing view:

// Now add the next button
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(self)];
self.navigationItem.rightBarButtonItem.tintColor = [UIColor blueColor];
self.navigationItem.rightBarButtonItem = nextButton;

The UINavigationController parent has this in viewDidLoad:

[super viewDidLoad];
// Do any additional setup after loading the view.

self.navigationBar.tintColor = [UIColor whiteColor];
self.navigationBar.barTintColor = [UIColor blackColor];

How do I just customize the rightBar button to change the background color and maybe the text color?

Answer

Roger C S Wernersson picture Roger C S Wernersson · Feb 17, 2014

You have to set the tintColor property on the button after you assign the button.

self.navigationItem.rightBarButtonItem = nextButton;
self.navigationItem.rightBarButtonItem.tintColor = [UIColor blueColor];

instead of

self.navigationItem.rightBarButtonItem.tintColor = [UIColor blueColor];
self.navigationItem.rightBarButtonItem = nextButton;