我基本上有一个ViewController,它是UINavigationController的根ViewController。基本上我有一个UIBarButtonItem,当用户按下它时,它的色调应该在红色和绿色之间切换。但颜色似乎没有改变。我的代码:
@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]];
}
}
答案 0 :(得分:0)
正如CSmith建议的那样,你应该直接设置色调:
- (void)changeColor {
colorMode = !colorMode;
if (colorMode) {
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor redColor]];
} else {
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor greenColor]];
}
}