问题
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.
回答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
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
回答2:
Adding the following line after disabling the button may help (not tested though)
self.navigationItem.rightBarButtonItem.tintColor = [UIColor lightGrayColor];
回答3:
You can set it from XIB/Storyboard like shown in image
or you can set the property - tintColor of your barbutton item.
回答4:
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
}
回答5:
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.
回答6:
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
来源:https://stackoverflow.com/questions/38864567/how-to-set-color-rightbarbuttonitem-on-navigation-bar