简体   繁体   English

按钮边框颜色不会更改为自定义颜色

[英]Button border color will not change to custom color

I have a very simple transparent button subclass, and I am trying to change the border to be the same color as the text, however it turns out white.我有一个非常简单的透明按钮子类,我正在尝试将边框更改为与文本相同的颜色,但它变成了白色。

class transparentButton: UIButton {

    required init?(coder aDecoder: NSCoder) {
       super.init(coder: aDecoder)
        setupButton()
    }
    
    // Setup the button
    func setupButton() {
        self.backgroundColor = .clear
        self.layer.borderWidth = 3
        self.layer.borderColor = CGColor(red: 203, green: 72, blue: 75, alpha: 0.75)
    }
    

}

The CGColor init takes in values from 0 to 1 , as the documentation says:文档所述, CGColor init 接受从01的值:

CGColor init的参数,表示每个值必须在0.0 t0 1.0之间

Instead of being out of 255 as you might be used to, the maximum value is 1 .最大值不是您可能习惯的255 ,而是1 If you supply a value that is over 1 , I assume it just caps down to 1 , which means that:如果您提供的值超过1 ,我认为它只是上限为1 ,这意味着:

CGColor(red: 203, green: 72, blue: 75, alpha: 0.75)

turns into...变成...

CGColor(red: 1, green: 1, blue: 1, alpha: 0.75)

...which is a white color. ...这是一种白色。

So, you must divide everything by 255 .因此,您必须将所有内容除以255

func setupButton() {
   self.backgroundColor = .clear
   self.layer.borderWidth = 3   
   self.layer.borderColor = CGColor(red: 203 / 255, green: 72 / 255, blue: 75 / 255, alpha: 0.75)
}
Before After
在此处输入图像描述 在此处输入图像描述

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

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