Ivan Rovelov
Ivan Rovelov

Reputation: 651

iOS: UIButton's title color when disabled

I'm using Mac OS X El Capitan 10.11.2, Xcode 7.1.1 and iOS 7 as a deployment target for the iOS application at hand.

Currently, I want to stylize a UIButton, so that it's text and background (color/image) is grayed out/has lowered alpha when disabled. I started playing with the control in the .xib editor and started changing the text color, background color and button image. Also, after each change of the aforementioned parameters I was altering the enabled state as well. The results were surprising. If the button's title color was set to be the default one - the blue one, then disabling the button from the .xib editor in Xcode results in grayed out button's title, which is great - no need to manually stylize the title in disabled state. The awkward behavior takes place when the button's title color is set to a value differing from the default one (green let's say) - no grayscaling, lowering alpha component or any other visual disabling effect is applied.

The result - one should manually apply visual disabling in case the button's title color differs from the default one. This is very bad partial behavior. No such thing is observed when using UILabel. No matter the text color the disabled grayed-out look is kept when the label is disabled. UIButton behaves as expected when it is given an image - the image has its alpha value set to a lower value when the button is disabled. Both UIButton and UILabel do not alter the background color if they are disabled.

What I want is to use the iOS automatic disabled UI look, so that there is visual consistency across all UI elements when they are disabled. I don't want to use the:

UIButton setTitleColor: (UIColor*) color forState: (UIControlState) state;

because don't want to manually specify the color for the disabled state but rather use automatic iOS color, image, etc. alternation in disabled state.

Also, is the specified erroneous behavior above some regression, or?

Upvotes: 2

Views: 8151

Answers (1)

rmaddy
rmaddy

Reputation: 318804

You can't. Once you call setTitleColor:forState: on the UIControlStateNormal state, that color applies to all states that don't have their own specific color.

The documentation for UIButton setTitleColor:forState: makes this clear:

In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.

So once you set a color for the Normal state, you need to set your desired colors for all other states such as the Disabled state.

Upvotes: 13

Related Questions