How to disable the highlight control state of a UIButton?

霸气de小男生 提交于 2019-11-29 19:29:01
Haydn

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.

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

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

KETAN
adjustsImageWhenHighlighted = NO;
Tim
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

therealbuckley

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.

}

For Swifty Developer -

yourButton.adjustsImageWhenHighlighted = false

Swift 3+

button.adjustsImageWhenHighlighted = false

button.adjustsImageWhenDisabled = false

just two things:

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

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!