UIButton角半径在CustomClass中无法正常工作

Bha*_*awa 2 cornerradius uibutton ios swift

我已经为UIButton小屏幕设备创建了一个自定义类,例如iPhone 5s cornerRadius无法正常工作

按钮看起来像这样

您对外观更接近看到UiButtoncornerRadius是不完全圆

class customRoundButton: UIButton
{
    override func awakeFromNib()
    {
        self.layer.cornerRadius = (self.layer.frame.height / 2)
        self.layer.borderColor = fontColor.defualtBlue.cgColor
        self.layer.borderWidth = 1
        self.layer.clipsToBounds = true
        self.layer.layoutIfNeeded()
    }
}

Run Code Online (Sandbox Code Playgroud)

And*_*tta 6

设置cornerRadiusin awakeFromNib为时过早。使用layoutSubviews来代替:

class CustomRoundButton: UIButton {

    override func awakeFromNib() {
        super.awakeFromNib()
        layer.borderColor = UIColor.blue.cgColor
        layer.borderWidth = 1
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        layer.cornerRadius = layer.frame.height / 2
    }

}
Run Code Online (Sandbox Code Playgroud)