简体   繁体   English

如何设置 UIButton 背景色的 alpha?

[英]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.我目前正在制作自定义按钮功能,但希望能够设置背景颜色的 alpha。 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
  }

To set the color with alpha use:要使用 alpha 设置颜色,请使用:

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

or:或者:

 button.backgroundColor?.withAlphaComponent(0.5)

在 Swift 5 中将 alpha 设置为 0.5 所需的代码是

button.alpha = 0.5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM