text color of disabled uibarbuttonitem is always the color of the normal state

僤鯓⒐⒋嵵緔 提交于 2020-01-02 05:33:55

问题


i wrote in my code

[[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];

but the text color is always blue despite the baritem being disabled.

I'm programming in xcode 5 and ios7


回答1:


for me following code works.

- (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];
}



回答2:


I found that it's because I set the navigationBar to unhidden after I called self.navigationItem.rightBarItem.enable. Putting the latter after the former fixes the problem




回答3:


I ran into the same problem and solved it by declaring the normal-button-state without any colorinformation.

Swift example:

    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)

just becomes

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

I'm not sure but I think it was because I declared also a tintColor in XCode.



来源:https://stackoverflow.com/questions/22550045/text-color-of-disabled-uibarbuttonitem-is-always-the-color-of-the-normal-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!