1

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?

cdub
  • 24,555
  • 57
  • 174
  • 303

4 Answers4

19

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;
Roger C S Wernersson
  • 6,362
  • 6
  • 34
  • 45
  • I am experiencing exactly this issue, trying to set my bar button items to [UIColor YellowColor] , but the assignment order and the code above does not work. – nwales Apr 02 '15 at 20:11
5

If you wish to have the same color for all bar buttons you can use the following line of code:

self.window.tintColor = [UIColor redColor];

in your app delegate.

fbernardo
  • 10,016
  • 3
  • 33
  • 46
shannoga
  • 19,649
  • 20
  • 104
  • 169
  • but that won't work if you just want the right one to change, correct? I probably have to create a button with an image and then cast that to a uibarbutton then – cdub Sep 30 '13 at 18:14
  • I am simply using self.navigationItem.leftBarButtonItem.tintColor = [UIColor redColor]; and it changes the button colour. can you explain further what are you doing ? – shannoga Sep 30 '13 at 19:50
4

Try below code you can set color of button as well text color

UIView *customBarButtonView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];

UIButton *buttonDone = [UIButton buttonWithType:UIButtonTypeSystem];
[buttonDone addTarget:self action:@selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

buttonDone.backgroundColor = [UIColor redColor];
   // or try this one
// buttonDone.tintColor = [UIColor redColor];

buttonDone.frame = CGRectMake(10, 0, 50, 30);

buttonDone.titleLabel.textColor = [UIColor colorWithRed:141.0/255.0 green:209.0/255.0 blue:205.0/255.0 alpha:1.0];
buttonDone.titleLabel.font = [UIFont fontWithName:@"SourceSansPro-Light" size:20.0f];
[buttonDone setTitle:@"Done" forState:UIControlStateNormal];
[customBarButtonView addSubview:buttonDone];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:customBarButtonView];
bhavya kothari
  • 7,484
  • 4
  • 27
  • 53
1

working code.. try this.

UIImage *image = [UIImage imageNamed:@"Image.png"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
self.navigationItem.leftBarButtonItem = menuButton;
yogesh wadhwa
  • 711
  • 8
  • 16