How to disable UIButton highlight on click?
Asked Answered
R

9

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?

Reconcile answered 8/7, 2013 at 13:50 Comment(3)
Are you using storyboards.Berlyn
when you say highlight effect, you mean that the text changes its colour?Caucasia
Also are you using an image for your button?Berlyn
R
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];
    }
}
Rosser answered 8/7, 2013 at 13:55 Comment(1)
#2260405Pallas
L
47

You could also make the UIButton type : custom from StoryBoard

Custom button

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

Leslee answered 22/3, 2014 at 6:16 Comment(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!Prolamine
this does not when using imagesBlalock
This is the answerProvitamin
K
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.

Kimber answered 11/6, 2015 at 10:14 Comment(0)
H
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

Hence answered 18/1, 2018 at 12:13 Comment(0)
R
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];
    }
}
Rosser answered 8/7, 2013 at 13:55 Comment(1)
#2260405Pallas
B
12

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

    overlayButton.adjustsImageWhenHighlighted = false
Bennir answered 5/4, 2017 at 17:51 Comment(1)
This should be marked as the correct answer, worked perfectly.Skantze
W
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!

Woodsy answered 13/5, 2015 at 16:40 Comment(0)
P
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 :(

Propraetor answered 22/7, 2017 at 19:0 Comment(0)
M
3

You can do it by creating extension in Swift 4.2

extension UIButton {
open override var isHighlighted: Bool {
    didSet {
        super.isHighlighted = false
    }
}}
Meit answered 24/1, 2019 at 10:19 Comment(0)
A
-1

I had this issue in a tableview and what resolved it for me was in cellForRowAt indexPath add the following:

For Swift 3 and above:

cell.selectionStyle = .none

Now when I click on the cell I no longer get a grey overlay on my cell.

Attract answered 8/11, 2023 at 18:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.