How to disable the highlight control state of a UIButton?

Viewed 91666

I've got a UIButton that, when selected, shouldn't change state when being touched. The default behaviour is for it to be in UIControlStateHighlighted while being touched, and this is making me angry.

Suggestions?

14 Answers

Your button must have its buttonType set to Custom.

In IB you can uncheck "Highlight adjusts image".

Programmatically you can use theButton.adjustsImageWhenHighlighted = NO;

Similar options are available for the "disabled" state as well.

button.adjustsImageWhenDisabled = NO;

is equally useful for having your own appearance of a disabled button.

Depending on what changes from the default to the highlighted state of the button, you can call a couple of methods to set them to what you need. So if the image changes you can do

[myButton setImage:[myButton imageForState:UIControlStateNormal] forState:UIControlStateHighlighted];

If the text changes you can do

[myButton setTitle:[myButton titleForState:UIControlStateNormal] forState:UIControlStateHighlighted];

other similar functions:

- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state

- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state

Swift 3+

button.adjustsImageWhenHighlighted = false

button.adjustsImageWhenDisabled = false

After the introduction of Style, you have to set the style to Default in IB along with setting the type to Custom to be able to disable the highlighting effect completely. Otherwise your button text will keep highlighting.

*Setting the Style to Default resets the text color to white.

Related