164

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?

jscs
  • 63,694
  • 13
  • 151
  • 195
kbanman
  • 4,223
  • 6
  • 32
  • 40

14 Answers14

334

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Haydn
  • 3,356
  • 1
  • 16
  • 2
  • 1
    Any chance you know what the programmatic version of this would be? Right now I've got it working hackishly by making it disabled, and dressing it up as being selected. – kbanman Feb 17 '10 at 06:50
  • 100
    uibutton.adjustsImageWhenHighlighted = NO; – Haydn Feb 20 '10 at 03:53
  • 44
    Also make sure the button type is set to CUSTOM. (As Mosib Asad mentioned in another answer) – Vlad Lego Jul 30 '14 at 16:04
  • Great!!! just a little and obvious change that i missed!!! I had set UIButton class type to MyCustomClass but forghet to change this one. So it was giving unselected button highlighted effect. Thank you once again. – Vinod Supnekar May 04 '17 at 06:38
41

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

Muhammad Asad
  • 1,013
  • 13
  • 11
39

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

Manish Ahuja
  • 4,509
  • 3
  • 28
  • 35
  • 1
    This works perfect for me. The `adjustsImageWhenHighlighted` property of `UIButton` seems to affect only the background image. – Michael Thiel May 07 '12 at 15:30
  • Be aware, that if do decide to assign an image to the 'disabled state', you do not call setBackgroundImage. Instead, you should do the following [button setImage:image forState:UIControlStateDisabled]; Also also be aware, that as is mentioned in this thread, the adjustsImageWhenHighlighted UIButton property only effects the background image. – HamasN Jul 22 '13 at 05:58
  • this wonderful answer should be referenced if your purpose is to remove the control's Highlight state , not only remove the highlight effect on click – ximmyxiao Oct 30 '20 at 03:34
28
adjustsImageWhenHighlighted = NO;
Luke
  • 11,426
  • 43
  • 60
  • 69
KETAN
  • 481
  • 1
  • 6
  • 14
23
button.adjustsImageWhenDisabled = NO;

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

Luke
  • 11,426
  • 43
  • 60
  • 69
Tim
  • 231
  • 2
  • 2
5

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

Dimitris
  • 13,480
  • 17
  • 74
  • 94
5

For Swifty Developer -

yourButton.adjustsImageWhenHighlighted = false
iDeveloper
  • 2,339
  • 2
  • 24
  • 38
4

Swift 3+

button.adjustsImageWhenHighlighted = false

button.adjustsImageWhenDisabled = false
yuanjilee
  • 429
  • 5
  • 16
3

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.

}
Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
3

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

Khushboo
  • 181
  • 1
  • 3
  • 13
2

just two things:

UIButton *btnTransparentComponent = [UIButton buttonWithType:UIButtonTypeCustom];
btnTransparentComponent.adjustsImageWhenHighlighted = NO;
maddy
  • 4,001
  • 8
  • 42
  • 65
2

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

enter image description here

Jeff
  • 840
  • 10
  • 29
1

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.

HAK
  • 2,023
  • 19
  • 26
-4

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

enter image description here

János
  • 32,867
  • 38
  • 193
  • 353