SwiftyJD
SwiftyJD

Reputation: 5441

How to set the alpha of the background color of a UIButton?

I'm currently making a custom button function but want to be able to set the alpha of the background color. This is what I have so far:

  func roundButtonCornersAndAddBorderColor(button: UIButton) {
    button.backgroundColor = .clear
    button.layer.cornerRadius = 5
    button.layer.borderWidth = 1
    button.layer.borderColor = UIColor.white.cgColor
  }

Upvotes: 3

Views: 7505

Answers (2)

Neo
Neo

Reputation: 303

In Swift 5 to set the alpha to 0.5 the code required is

button.alpha = 0.5

Upvotes: 4

DiegoQ
DiegoQ

Reputation: 1116

To set the color with alpha use:

UIColor(red: 0.5, green: 0.5, blue:0, alpha: 1.0)

or:

 button.backgroundColor?.withAlphaComponent(0.5)

Upvotes: 6

Related Questions