简体   繁体   中英

How to set color `rightBarButtonItem` on navigation bar?

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

在此输入图像描述

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.

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 设置球棒按钮颜色

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

禁用按钮后添加以下行可能会有所帮助(尽管未测试)

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

You can set it from XIB/Storyboard like shown in image 设置栏按钮颜色

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

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
}

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM