如何设置UIButton背景色的Alpha?

我目前正在做一个自定义按钮功能,但希望能够设置背景颜色的阿尔法。这就是我目前所拥有的:

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


最佳答案:

要使用alpha设置颜色:

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

或:
 button.backgroundColor?.withAlphaComponent(0.5)