8

I have a UIButton, I want to disable its UIControlStateHighlighted if the button is in selected state. With that I mean, if the current state of UIButton is ControlStateSelected then on touch down, its state should not change to highlighted which is the default behavior of a UIButton.

jscs
  • 63,694
  • 13
  • 151
  • 195
Manish Ahuja
  • 4,509
  • 3
  • 28
  • 35
  • Possible duplicate of [How to disable the highlight control state of a UIButton?](http://stackoverflow.com/questions/2259905/how-to-disable-the-highlight-control-state-of-a-uibutton) – jscs Jun 14 '16 at 00:36

4 Answers4

19
[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];

Third line is the trick here, it will disable the highlighted state of UIButton if button is already in Selected State

Manish Ahuja
  • 4,509
  • 3
  • 28
  • 35
  • 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
9

Uncheck "highlight adjusts image" in IB, Also make sure that button type is set CUSTOM in IB

Muhammad Asad
  • 1,013
  • 13
  • 11
3
if(button.selected == YES)
button.adjustsImageWhenHighlighted = NO;
else
button.adjustsImageWhenHighlighted = YES;

Hope this helps

visakh7
  • 26,380
  • 8
  • 55
  • 69
1

just two things:

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