禁用的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];
但文字颜色始终是蓝色,尽pipe禁用了baritem。
我在xcode 5和ios7编程
对于我下面的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]; }
我发现这是因为我设置navigationBar后,我叫self.navigationItem.rightBarItem.enable隐藏。 把后者放在前者后解决问题
我碰到了同样的问题,并通过声明正常的button状态而没有任何颜色信息来解决它。
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
。