Manish Ahuja
Manish Ahuja

Reputation: 4517

Disable highlighted UIControl state of a UIButton

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.

Upvotes: 8

Views: 8024

Answers (4)

maddy
maddy

Reputation: 4111

just two things:

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

Upvotes: 1

Muhammad Asad
Muhammad Asad

Reputation: 1013

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

Upvotes: 9

Manish Ahuja
Manish Ahuja

Reputation: 4517

[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

Upvotes: 19

visakh7
visakh7

Reputation: 26390

if(button.selected == YES)
button.adjustsImageWhenHighlighted = NO;
else
button.adjustsImageWhenHighlighted = YES;

Hope this helps

Upvotes: 3

Related Questions