Cuong Nguyen
Cuong Nguyen

Reputation: 990

How to set color `rightBarButtonItem` on navigation bar?

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.

Upvotes: 10

Views: 14045

Answers (6)

Hula
Hula

Reputation: 71

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

Upvotes: 4

Dishant Rajput
Dishant Rajput

Reputation: 1357

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
}

Upvotes: 2

Cuong Nguyen
Cuong Nguyen

Reputation: 990

Thanks guys for support me. I also change the code like you but text color of "Done" button can not change. Now, I've found a good solution for this issue.

Firstly, I create a common function like that:

- (void)changeStatusOfRightBarButton:(BOOL)enabled withColorAlpha:(CGFloat)numberAlpha {
    _doneButton.enabled = enabled;
    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
    setTitleTextAttributes:
    @{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"FFFFFF" alpha:numberAlpha],
    NSFontAttributeName:[UIFont fontAvenirNextRegularWithSize:15.0f]
    }
    forState:UIControlStateNormal];

}

and secondly I apply it into viewDidload like that:

if (self.noteTextView.text.length == 0) {
    [self.noteTextView becomeFirstResponder];
    [self changeStatusOfRightBarButton:NO withColorAlpha:0.44f];

}

When I apply these code "Done" button has changed color.

Upvotes: 0

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

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

Upvotes: 11

Raj Aggrawal
Raj Aggrawal

Reputation: 761

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.

Upvotes: 1

DAXaholic
DAXaholic

Reputation: 35338

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

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

Upvotes: 2

Related Questions