0

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

1 回答 1

0

正如 CSmith 所建议的,您应该直接设置色调:

- (void)changeColor {
    colorMode = !colorMode;

    if (colorMode) {
        [self.navigationItem.rightBarButtonItem setTintColor:[UIColor redColor]];
    } else {
        [self.navigationItem.rightBarButtonItem setTintColor:[UIColor greenColor]];
    }
}
于 2012-10-09T13:43:39.873 回答