我基本上有一个 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]];
}
}