How to disable the highlight control state of a UIButton?

IosCocoa TouchUibuttonUikitUicontrol

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

In addition to above answer of unchecking "highlight adjusts image" in IB, make sure that button type is set CUSTOM.

Solution 3 - Ios

This will work for you:

[button setBackgroundImage:[UIImage imageNamed:@"button_image"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"button_image_selected"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:@"button_image_selected"] forState:UIControlStateSelected | UIControlStateHighlighted];

3rd line is the trick here...

This works the same for setting image/backgroundImage

Solution 4 - Ios

adjustsImageWhenHighlighted = NO;

Solution 5 - Ios

button.adjustsImageWhenDisabled = NO;

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

Solution 6 - Ios

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

Solution 7 - Ios

For Swifty Developer -

yourButton.adjustsImageWhenHighlighted = false

Solution 8 - Ios

Swift 3+

button.adjustsImageWhenHighlighted = false

button.adjustsImageWhenDisabled = false

Solution 9 - Ios

OK here's an easy solution if this works for you, after a week of banging my head on this it finally occurred to me to just set highlighted=NO for the 1st line of the IBAction method for the TouchUpInside or TouchDown, or whatever works. For me it was fine on the TouchUpInside.

-(IBAction)selfDismiss:(id)sender {

    self.btnImage.highlighted = NO;

    NSLog(@"selfDismiss");

    etc, etc, etc.

}

Solution 10 - Ios

make your button Type - "Custom" and Uncheck - Highlighted Adjust image and you are done.

Solution 11 - Ios

just two things:

UIButton *btnTransparentComponent = [UIButton buttonWithType:UIButtonTypeCustom];
btnTransparentComponent.adjustsImageWhenHighlighted = NO;

Solution 12 - Ios

I had a similar issue and found that "unchecking" Clears Graphic Content in interface builder fixed my issue

enter image description here

Solution 13 - Ios

avoid to set UIButton's Line Break to Clip, use instead the standard Truncate Middle

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkbanmanView Question on Stackoverflow
Solution 1 - IosHaydnView Answer on Stackoverflow
Solution 2 - IosMuhammad AsadView Answer on Stackoverflow
Solution 3 - IosManish AhujaView Answer on Stackoverflow
Solution 4 - IosKETANView Answer on Stackoverflow
Solution 5 - IosTimView Answer on Stackoverflow
Solution 6 - IosDimitrisView Answer on Stackoverflow
Solution 7 - IosiDeveloperView Answer on Stackoverflow
Solution 8 - IosyuanjileeView Answer on Stackoverflow
Solution 9 - IostherealbuckleyView Answer on Stackoverflow
Solution 10 - IosKhushbooView Answer on Stackoverflow
Solution 11 - IosmaddyView Answer on Stackoverflow
Solution 12 - IosJeffView Answer on Stackoverflow
Solution 13 - IosJánosView Answer on Stackoverflow