繁体   English   中英

按钮角半径在 iOS 中不起作用

[英]button corner radius is not working in iOS

我使用下面的代码给按钮角redius,但它不是工作

button.layer.cornerRadius = 3
button.layer.borderWidth = 1

此代码无效。请帮助我

我认为您需要将按钮属性 clipsToBounds 设置为 true

button.clipsToBounds = true

在提到的两个语句之前添加以下代码行。

button.layer.masksToBounds = true

UIView漂亮扩展:

extension UIView {

   func setCornerRadius(amount: CGFloat, withBorderAmount borderWidthAmount: CGFloat, andColor borderColor: UIColor) {

        self.layer.cornerRadius = amount
        self.layer.borderWidth = borderWidthAmount
        self.layer.borderColor = borderColor.cgColor
        self.layer.masksToBounds = true

    }
}

用法:

button.setCornerRadius(amount: 2.0, withBorderAmount: 2.0, andColor: .blue)

在提到的两个语句之前添加以下代码行。

buttonProfile.clipsToBounds = true
buttonProfile.layer.cornerRadius = buttonProfile.frame.size.width / 2

它总是对我有用

    func setCornerTo(_ button : UIButton , withRadius radius : CGFloat) 
    {
        button.layer.cornerRadius = radius
        button.clipsToBounds = true
    }

并像这样打电话。 .

setCornerTo( myButton , withRadius: 5)

暂无
暂无

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

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