How to set color `rightBarButtonItem` on navigation bar?

12,081

Solution 1

for change the color of barbutton use

objective C

self.navigationItem.rightBarButtonItem = yourRightbarbuttonName;
self.navigationItem.rightBarButtonItem.tintColor = [UIColor blueColor]; // add your color

Swift

or set from Story board Set bat button color

self.navigationItem.rightBarButtonItem = yourRightbarbuttonName
self.navigationItem.rightBarButtonItem.tintColor = UIColor.blueColor()

if you want to show the Right barbutton use

objective C

self.navigationItem.rightBarButtonItem.enabled = YES;

Swift

self.navigationItem.rightBarButtonItem.enabled = true

if you want to hide the Right barbutton use

objective C

self.navigationItem.rightBarButtonItem.enabled = NO;

Swift

self.navigationItem.rightBarButtonItem.enabled = false

Solution 2

Adding the following line after disabling the button may help (not tested though)

self.navigationItem.rightBarButtonItem.tintColor = [UIColor lightGrayColor];

Solution 3

Try This

UIBarButtonItem *rightBarButton=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(editProfileClick)];
rightBarButton.tintColor=[UIColor whiteColor];
self.navigationItem.rightBarButtonItem = rightBarButton;


-(void)editProfileClick
{
//your methods
}

Solution 4

For updated the colour for text of right bar item, we should use "setTitleTextAttributes"(swift 5). Please try this :

let rightBarButton = UIBarButtonItem(title: "your text", style: .plain, target: nil, action: nil)
rightBarButton.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.black], for: .normal)
navigationItem.rightBarButtonItem = rightBarButton

Solution 5

You can set it from XIB/Storyboard like shown in imageSet bar button color

or you can set the property - tintColor of your barbutton item.

Share:
12,081
Cuong Nguyen
Author by

Cuong Nguyen

THE SUN RAISE: I'm working in my company. At here, I spent all time for developing iOS application. THE SUN GOES DOWN: I'm coding app for iOS that my feeling cool and reading some article about programming software. FOR FUN: Ruby on Rails, Javascript, Game, Football... Many things that make mine for fun.

Updated on July 18, 2022

Comments

  • Cuong Nguyen
    Cuong Nguyen almost 2 years

    I want to set color of "Done" button on navigation bar like this image:

    enter image description here

    Although, I've set code as self.doneButton.enabled = NO; but Done button still has a white color. It does not change color like image.

    How to set the code to change text color done button like Done button in the image above? Please help me to solve this problem. I appreciate your help.