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?

asked Jul 8, 2013 at 13:50
3
  • Are you using storyboards. Commented Jul 8, 2013 at 13:53
  • when you say highlight effect, you mean that the text changes its colour? Commented Jul 8, 2013 at 13:58
  • Also are you using an image for your button? Commented Jul 8, 2013 at 14:04

10 Answers 10

47

You could also make the UIButton type : custom from StoryBoard

Custom button

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

answered Mar 22, 2014 at 6:16
Sign up to request clarification or add additional context in comments.

3 Comments

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!
this does not when using images
This is the answer
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.

answered Jun 11, 2015 at 10:14

Comments

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

answered Jan 18, 2018 at 12:13

Comments

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];
 }
}
answered Jul 8, 2013 at 13:55

1 Comment

12

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

 overlayButton.adjustsImageWhenHighlighted = false
answered Apr 5, 2017 at 17:51

1 Comment

This should be marked as the correct answer, worked perfectly.
6

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!

answered May 13, 2015 at 16:40

Comments

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 :(

answered Jul 22, 2017 at 19:00

Comments

3

You can do it by creating extension in Swift 4.2

extension UIButton {
open override var isHighlighted: Bool {
 didSet {
 super.isHighlighted = false
 }
}}
answered Jan 24, 2019 at 10:19

Comments

0

UIButton.Configuration API (iOS 15+)

class MyButton : UIButton {
 required init?(coder: NSCoder) {
 super.init(coder: coder)
 
 var configuration = self.configuration!
 configuration.background.backgroundColorTransformer = UIConfigurationColorTransformer { color in
 return configuration.baseBackgroundColor ?? .clear
 }
 configuration.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { container in
 container.merging(AttributeContainer([
 .foregroundColor: configuration.baseForegroundColor ?? .white
 ]))
 }
 self.configuration = configuration
 }
}
answered Sep 3, 2025 at 15:56

1 Comment

Please don't post identical answers to multiple questions. Post one good answer, then vote/flag to close the other questions as duplicates. If the question is not a duplicate, tailor your answers to the question.
-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.

answered Nov 8, 2023 at 18:46

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.