TommyG
TommyG

Reputation: 4155

How to remove UIButton selected background color?

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;

Upvotes: 36

Views: 29838

Answers (14)

Matt
Matt

Reputation: 51

Using a UIButtonConfiguration, I had to make the following call:

btnCfg.baseBackgroundColor = [UIColor clearColor];

Upvotes: 0

dimaskpz
dimaskpz

Reputation: 87

this will remove your UIButton background color when selected

@IBOutlet weak var submitButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    let button = UIButton(type: .custom)
    submitButton = button
    submitButton.tintColor = .clear 
}

Upvotes: -1

Mitesh
Mitesh

Reputation: 534

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

Upvotes: 1

Leon
Leon

Reputation: 3746

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
}

Upvotes: 3

JobLess
JobLess

Reputation: 67

Changed UIButton type to Custom in IB worked for me.

Upvotes: 3

Akash Bhardwaj
Akash Bhardwaj

Reputation: 328

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

Upvotes: 11

Jack
Jack

Reputation: 14379

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

Upvotes: 5

Sasa Blagojevic
Sasa Blagojevic

Reputation: 2200

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

Upvotes: 1

jk2K
jk2K

Reputation: 4567

Swift Version

extension UIButton {
    override public var highlighted: Bool {
        didSet {
            // clear background color when selected
            self.layer.backgroundColor = .None
        }
    }
}

Upvotes: 0

Rufus
Rufus

Reputation: 2078

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.

Upvotes: 134

Ali Seymen
Ali Seymen

Reputation: 801

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;

Upvotes: 11

JDx
JDx

Reputation: 2655

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.

Upvotes: -2

sudo rm -rf
sudo rm -rf

Reputation: 29524

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];
}

Upvotes: 22

Simon Lee
Simon Lee

Reputation: 22334

Use...

    [myButton setAdjustsImageWhenHighlighted:FALSE];

Upvotes: -2

Related Questions