iOS:Can';不要改变uibarbuttonite的颜色
我基本上有一个ViewController,它是UINavigationController的根ViewController。基本上我有一个uibarbuttonite,当用户按下它时,它的色调应该在红色和绿色之间切换。但是颜色似乎没有改变。我的代码:iOS:Can';不要改变uibarbuttonite的颜色,ios,ios5,uibarbuttonitem,uiappearance,Ios,Ios5,Uibarbuttonitem,Uiappearance,我基本上有一个ViewController,它是UINavigationController的根ViewController。基本上我有一个uibarbuttonite,当用户按下它时,它的色调应该在红色和绿色之间切换。但是颜色似乎没有改变。我的代码: @interface TestButtonColorViewController (){ BOOL colorMode; } @end @implementation TestButtonColorViewController - (
@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]];
}
}
正如CSmith所建议的,您应该直接设置色调:
- (void)changeColor {
colorMode = !colorMode;
if (colorMode) {
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor redColor]];
} else {
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor greenColor]];
}
}
UIAppearance方法仅用于在创建控件时为控件添加颜色,而不是在创建之后。