禁用uibarbuttonitem的文本颜色始终是正常状态的颜色

时间:2014-03-21 04:16:43

标签: ios uibarbuttonitem

我在我的代码中写道

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]  setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSFontAttributeName:[UIFont boldSystemFontOfSize:16.0f]} forState:UIControlStateDisabled];

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]  setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blueColor], NSFontAttributeName:[UIFont boldSystemFontOfSize:16.0f]} forState:UIControlStateNormal];

但是,尽管禁用了该项目,但文本颜色始终为蓝色。

我正在编写xcode 5和ios7

3 个答案:

答案 0 :(得分:4)

对我来说code工作。

- (void)viewDidLoad {
    [super viewDidLoad];

    UIBarButtonItem * btnTemp = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(btnDone_Click:)];
    [btnTemp setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSFontAttributeName:[UIFont boldSystemFontOfSize:16.0f]} forState:UIControlStateNormal];

    [btnTemp setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blueColor], NSFontAttributeName:[UIFont systemFontOfSize:16.0f]} forState:UIControlStateDisabled];


    [self.navigationItem setRightBarButtonItem:btnTemp];

}

- (void)btnDone_Click : (id)sender {

    UIBarButtonItem * button = (UIBarButtonItem *)sender;
    [button setEnabled:FALSE];
    [self performSelector:@selector(enableButton:) withObject:sender afterDelay:2.0f];


}

- (void)enableButton : (id)sender {
    UIBarButtonItem * button = (UIBarButtonItem *)sender;
    [button setEnabled:TRUE];
}

答案 1 :(得分:3)

我发现这是因为我在调用self.navigationItem.right Bar Item.enable后将导航栏设置为unhidden。将后者置于前者之后可以解决问题

答案 2 :(得分:2)

我遇到了同样的问题并通过声明没有任何颜色信息的正常按钮状态来解决它。

Swift示例:

    myBtn.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "xxx", size: 20.0)!, NSForegroundColorAttributeName: UIColor.whiteColor()], forState: UIControlState.Normal)
    myBtn.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "xxx", size: 20.0)!, NSForegroundColorAttributeName: UIColor.darkGrayColor()], forState: UIControlState.Disabled)

变成

    myBtn.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "xxx", size: 20.0)!], forState: UIControlState.Normal)

我不确定,但我认为这是因为我在XCode中声明了tintColor