36

I am looking for a solution to this problem that does NOT require using images/PNGs. I am looking for a way to remove the UIButton's blue background color when it's in selected state, and I just cannot find a way to do that without using images. I am basically trying to do something like that in case of a UITableViewCell:

   cell.selectionStyle = UITableViewCellSelectionStyleNone;
Zorayr
  • 23,770
  • 8
  • 136
  • 129
TommyG
  • 4,145
  • 10
  • 42
  • 66
  • Wow, just tried all of these answers on a custom button and could get none of them to work. – Tim MB Aug 17 '19 at 10:46

12 Answers12

132

I had this same issue and I resolve it by changing the UIButton type from "System" to "Custom". The blue background color does not show up in selected state when it is a "Custom" type.

Rufus
  • 2,058
  • 2
  • 16
  • 10
21

Unfortunately I'm away from my test machine at the moment, but there are two things you could try.

First would be to set the following property:

button.adjustsImageWhenHighlighted = NO;

Or uncheck "Highlight adjusts image" in Interface Builder.

If that doesn't work like you expect it to, you can deselect the button in the action you tied it to, like so:

- (IBAction)yourButtonAction:(id)sender {
    [sender setHighlighted:NO];
}
sudo rm -rf
  • 29,408
  • 19
  • 102
  • 161
  • does it but not in a clean way - when I click, you still see the blue color for a brief moment. I am hoping to find a solution that will eliminate that completely. – TommyG Jan 09 '12 at 15:28
  • Then why don't you just create your own `UIControl`? It's not that hard. Then you can customize it to your heart's content. – sudo rm -rf Jan 09 '12 at 15:39
  • Still Works great, ios:9.3, just `button.adjustsImageWhenHighlighted = NO` is enough. – Amr Lotfy Nov 23 '16 at 06:35
  • 3
    I set `tintColor` property of `UIButton` to `clear` it removed the background blue color when selected – Mohammad Azam Aug 26 '20 at 11:20
10

Change the alpha of the tintColor to zero, works if iOS is 5.0 or later

UIColor *color = [[UIColor alloc] initWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];  
button.tintColor = color;
Ali Seymen
  • 771
  • 6
  • 9
9

Very simple, in storyBoard, select your UIButton and open attribute inspector. Now scroll down to View section and change Tint property to Clear Color or any specific color if u want.

enter image description here

Akash Bhardwaj
  • 308
  • 2
  • 11
  • I know your answer is correct, but unfortunately after updating the Xcode, this is not working for new version of Xcode 13. Can you explain what does happened with the new Xcode? I want to remove the background color and tint color both, because I have a button on top of big uiview.! – Muhammad Danish Qureshi Oct 21 '21 at 07:08
5

UIButton also with custom type, we can set it in xcode as-

enter image description here

OR By programmatically as-

 let customButton = UIButton(type: .custom) 
  customButton.setTitle("this is Button", for: .normal)
  customButton.setTitleColor(UIColor.blue, for: .normal)
  customButton.frame = CGRect(x: 15, y: 50, width: 300, height: 500)
            self.view.addSubview( customButton)

Now there is no more UIButton's blue background color

Jack
  • 13,571
  • 6
  • 76
  • 98
3

Not a direct answer to OP's question but to remove this colour in a UIButton subclass you can do this:

override func layoutSubviews() {
    super.layoutSubviews()
    tintColor = .clear
}
Leon
  • 3,614
  • 1
  • 33
  • 46
3

Changed UIButton type to Custom in IB worked for me.

JobLess
  • 67
  • 3
1

What did it for me was changing the button.tintColor in the highlighted state. This is swift 3, iOS 10

override public var isHighlighted: Bool {
    didSet {
        self.tintColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1.0)
    }
}

Or you can do the same in the interface builder

Choose button state

Choose button tint color

Sasa Blagojevic
  • 2,110
  • 17
  • 22
0

Swift Version

extension UIButton {
    override public var highlighted: Bool {
        didSet {
            // clear background color when selected
            self.layer.backgroundColor = .None
        }
    }
}
jk2K
  • 4,279
  • 3
  • 37
  • 40
0

From iOS 15 onward, UIButton has property configuration. You can set clear color to property baseBackfroundColor to remove background color of UIButton in selected mode.

var config = UIButton.Configuration.plain()
config.baseBackgroundColor = .clear
button.configuration = config
Mitesh
  • 504
  • 2
  • 8
-2

Use...

    [myButton setAdjustsImageWhenHighlighted:FALSE];
Simon Lee
  • 22,304
  • 4
  • 41
  • 45
-2

Not sure if this is the best way. But you could have an invisible button or view ontop of the actual button then recognize when the top button/view has been clicked.

JDx
  • 2,615
  • 3
  • 22
  • 33