Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update UIBarbuttonItem font when disabled - iOS 11

Upto iOS 10, the font for disabled and enabled uibarbuttonitem remains same, only color differs. But, after i installed my app on device having ios 11, the font for disabled mode get updated(showing system font), whereas in enabled mode it is showing the proper font which i set.

So, for case of iOS 11, How can i set font for disabled mode to keep consistency in the app.

like image 904
Mehul Thakkar Avatar asked Sep 26 '17 12:09

Mehul Thakkar


1 Answers

This appears to have changed in iOS 11, at least in my case where I'm using the UIAppearance protocol. Not sure if this is a bug or intentional.

I also found I couldn't mask values together (such as .normal|.disabled) as it meant it would only apply the font if the control satisfied all states.

So I ended up doing this:

for controlState in [UIControlState.normal, UIControlState.disabled, UIControlState.focused, UIControlState.highlighted, UIControlState.selected] {
    barButton.setTitleTextAttributes([NSFontAttributeName: customFontName], for: controlState)
}

For updating it everywhere using the UIAppearance protocol:

for controlState in [UIControlState.normal, UIControlState.disabled, UIControlState.focused, UIControlState.highlighted, UIControlState.selected] {
    UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: customFontName, for: controlState);
}
like image 135
SeanR Avatar answered Nov 08 '22 11:11

SeanR