Reputation: 990
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.
Upvotes: 10
Views: 14045
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
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
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
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
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
Reputation: 761
You can set it from XIB/Storyboard like shown in image
or you can set the property - tintColor of your barbutton item.
Upvotes: 1
Reputation: 35338
Adding the following line after disabling the button may help (not tested though)
self.navigationItem.rightBarButtonItem.tintColor = [UIColor lightGrayColor];
Upvotes: 2