I basically have a ViewController that is the root ViewController of a UINavigationController. Basically I have a UIBarButtonItem that when a user presses on it, its tint color should toggle between red and green. But the color doesn't seem to change. My code:
@interface TestButtonColorViewController (){
BOOL colorMode;
}
@end
@implementation TestButtonColorViewController
- (void)viewDidLoad
{
[super viewDidLoad];
colorMode = NO;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Change" style:UIBarButtonItemStyleBordered target:self action:@selector(changeColor)];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)changeColor{
colorMode = !colorMode;
if (colorMode) {
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTintColor:[UIColor redColor]];
}
else {
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTintColor:[UIColor greenColor]];
}
}
As CSmith has suggested, you should set the tint directly:
- (void)changeColor {
colorMode = !colorMode;
if (colorMode) {
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor redColor]];
} else {
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor greenColor]];
}
}
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.