UIBarButtonItem is disabled, but has normal color

坚强是说给别人听的谎言 提交于 2019-12-03 11:25:58
Swift Hipster

Swift 4

If someone looking for how to change barbuttonitem disabled state appearance in swift. Here you go.

barButtonItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.lightGray], for: .disabled)

Check with following 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];
}

So I finally managed to get this working as it should, and the problem was that I was setting the color of UIBarButtonItems twice, once using [navBar setTintColor:] and once using the appearance proxy. Leaving just the appearance proxy solves the problem.

You have likely set the bar button item title text attributes for the .Normal state and need to also set it for the .Disabled state.

There are two ways to fix this, one if you are setting the title text attributes on the bar button item instance and in the other case if you use the appearance proxy.

Single instance:

saveButton.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.grayColor()], forState: .Disabled)

Appearance proxy:

UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([MyNavigationController.self]).setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.grayColor()], forState: .Disabled)

This updates the answer for Swift 4.0:

barButtonItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.gray], for: UIControlState.disabled)

This shows an illustrative example of an orange color for disabled against a white barTintColor:

barTintColor = .white    
cancelButton.isEnabled = false
cancelButton.setTitleTextAttributes(
    [NSAttributedStringKey.foregroundColor: UIColor.orange], 
    for: UIControlState.disabled)

This was also my issue, when running on iOS versions 10.*. Setting the button's foreground color solved the issue for me.

self.saveButton.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.gray], for: UIControlState.disabled)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!