简体   繁体   English

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

[英]UIButton corner radius not working properly in CustomClass

I've make a custom class for UIButton but in small screen devices eg iPhone 5s cornerRadius not working properly我为UIButton创建了一个自定义类,但在小屏幕设备中,例如iPhone 5s cornerRadius无法正常工作

按钮看起来像这样

You've to looks closer to see the UiButton 's cornerRadius is not perfectly rounded你必须仔细观察才能看到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()
    }
}

Setting the cornerRadius in awakeFromNib is too early.awakeFromNib设置cornerRadius为时尚早。 Use layoutSubviews instead:使用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
    }

}

Try尝试

override func layoutSubviews() {
        super.layoutSubviews()
        self.layer.cornerRadius = self.bounds.height * 0.50    
}

So by the time your custom UIButton is going to be added to the subviews it will know it bounds and you just have to assign the corner radius to be the half of bounds height.因此,当您的自定义 UIButton 将被添加到子视图时,它会知道它的边界,您只需将角半径指定为边界高度的一半。

Better use IBDesignable for this :最好为此使用IBDesignable

@IBDesignable
class RoundedButton: UIButton {

    @IBInspectable var cornerRadius: CGFloat = 3.0 {
        didSet {
            self.layer.cornerRadius = cornerRadius
        }
    }

    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        self.setupView()
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        self.setupView()
    }

    func setupView() {
        self.layer.masksToBounds = false
        self.layer.cornerRadius = cornerRadius
    }
}

I stumbled upon this question here and found for me what worked best in Swift 5 is setting the corner radius in awakeFromNib inside Disptatch.main.async When using layoutSubviews or setNeedsLayout the closure is called for every little change or movement.我在这里偶然发现了这个问题, Disptatch.main.async Swift 5 中最有效的awakeFromNibDisptatch.main.async中的awakeFromNib设置拐角半径 当使用layoutSubviewssetNeedsLayout ,每次小的变化或移动都会调用闭包。 This might work for some use cases.这可能适用于某些用例。 I have not tried that with the above example.我没有用上面的例子尝试过。

My code looks like this:我的代码如下所示:

override func awakeFromNib() {
    super.awakeFromNib()
    imageView.layer.masksToBounds = false
    imageView.clipsToBounds = true
        
    DispatchQueue.main.async {
        self.imageView.layer.cornerRadius = self.imageView.bounds.height / 2.0
        }
    }

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

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