28

How to disable button highlight effect on click? Have custom button with white bg color and DarkGray text color. The problem is the text becomes white on button click. Already tried but none of them worked:

a. Did uncheck "Highlighted Ajusts Image" in interface builder.

b. Tried setting highlighted = NO in button pressed method:

((UIButton *)sender).highlighted = NO

c. Tried setting the same title for highlihted state:

[button setTitle:[button titleForState:UIControlStateNormal] forState:UIControlStateHighlighted];

Any suggestions?

Centurion
  • 14,106
  • 31
  • 105
  • 197

8 Answers8

47

You could also make the UIButton type : custom from StoryBoard

Custom button

This should strip down any system behavior for you. (I think)

Paweł Brewczynski
  • 2,665
  • 3
  • 30
  • 43
  • 3
    After quite some searching I came upon your answer. Adds an important aspect which solved my problem: The Type=System behavior will add a fade effect to the highlighted background image and text. Switching to Type=Custom will disable this effect so that exactly the colors specified will be displayed. Thanks! – Christian Aug 08 '14 at 13:24
  • 2
    this does not when using images – anders Aug 18 '14 at 16:14
  • This is the answer – visc Mar 29 '19 at 15:12
25

This is a solution for buttons that have images. In the storyboard, there is a property called HighlightedAdjustsImage which is On by default. Disable that and your button image will not show any highlighted properties.

Rajeev Bhatia
  • 2,974
  • 1
  • 21
  • 29
22

Swift 4 with XCode 9

Just uncheck "Highlighted Adjust Image" and if you want to remove disabled background uncheck "Disabled Adjust Image" too.

First ensure your button type is "Custom"

enter image description here

ibrahimyilmaz
  • 2,317
  • 1
  • 24
  • 28
12

UIButton will highlighted on click, so check button setting Change the title color in highlight state config to same as default state Or you can set:

[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];

If you want to control Highlighted by code, you can disable normal highlighted by subclass Button and disable in touchesBegin:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    if (self.state == UIControlStateHighlighted) {
        [self setHighlighted:NO];
    }
}
LE SANG
  • 10,955
  • 7
  • 59
  • 78
12

old outdated answers, and old question, but the newest and easiest way is:

    overlayButton.adjustsImageWhenHighlighted = false
Jeremie D
  • 4,145
  • 1
  • 35
  • 41
7

None of the solutions I found online worked for me (including these ones listed here), so I ended up doing this in my custom UIButton subclass:

// swift:

override var isHighlighted: Bool {
    didSet { if isHighlighted { isHighlighted = false } }
}

I also have the buttonType set to .custom, but it probably makes no difference here.

And now: no more weird highlighting or colour animations!

ephemer
  • 1,239
  • 8
  • 21
3

My 2 cents ;) Swift 3 ready

class CustomButton: UIButton {

    override var isHighlighted: Bool {
        didSet {
            if (isHighlighted) {
                super.isHighlighted = false
            }
        }
    }

}

Situation and History

I had set images for UIControlState.normal and UIControlState.selected, so setting UIButton.isSelected = true shows selected image, else the normal image.

The issue we discovered was when UIButton.isSelected = true, and during highlight/focus (by tap hold the button and discard tap by swiping away) button shows image for UIControlState.normal, that looked pretty ugly to my boss :D

I tried several solutions, such as UIButton(type: UIButtonType.custom) and UIButton.adjustsImageWhenHighlighted = false, none helped :(

AamirR
  • 11,672
  • 4
  • 59
  • 73
3

You can do it by creating extension in Swift 4.2

extension UIButton {
open override var isHighlighted: Bool {
    didSet {
        super.isHighlighted = false
    }
}}
iOS Lifee
  • 2,091
  • 23
  • 32