[英]Corners not rounding for button in UITableViewCell
I'm trying to round a button's bottom corners by calling this method in layoutSubviews
for a UITableViewCell
: 我试图通过在
UITableViewCell
layoutSubviews
调用此方法来layoutSubviews
按钮的底角:
func styleRescueButton() {
let rescueButtonCornerLayer = CAShapeLayer()
let rescueButtonRoundedCornersPath = UIBezierPath(roundedRect: rescueNowButton.bounds, byRoundingCorners: [UIRectCorner.bottomLeft, UIRectCorner.bottomRight], cornerRadii: CGSize(width: MyTheme.cornerRadius, height: MyTheme.cornerRadius))
rescueButtonCornerLayer.path = rescueButtonRoundedCornersPath.cgPath
rescueNowButton.layer.mask = rescueButtonCornerLayer
rescueNowButton.clipsToBounds = true
rescueNowButton.layer.masksToBounds = true
}
it's not working at all though, the corners are not rounding. 它根本不工作,角落也不圆。 What am I doing wrong here?
我在这里做错了什么?
You can define an extension: 您可以定义扩展名:
extension UIButton{
func bottomRoundButton(){
let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners [.BottomLeft , .BottomRight], cornerRadii:CGSizeMake(5.0, 5.0))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.bounds
maskLayer.path = maskPath.CGPath
self.layer.mask = maskLayer
}
}
You can do the same things for other corner as well. 您也可以为其他角落做同样的事情。 Just change
byRoundingCorners [.BottomLeft , .BottomRight]
只需通过
byRoundingCorners [.BottomLeft , .BottomRight]
更改
The issue with your code is that you never set mask's frame. 代码的问题在于您从未设置遮罩的框架。 Also it's not necessary to set
clipsToBounds
and layer.masksToBounds
: 同样也没有必要设置
clipsToBounds
和layer.masksToBounds
:
These lines can be removed: 这些行可以删除:
rescueNowButton.clipsToBounds = true
rescueNowButton.layer.masksToBounds = true
Working function in Swift 3: Swift 3中的工作功能:
func styleRescueButton() {
let maskLayer = CAShapeLayer()
maskLayer.frame = rescueNowButton.bounds
maskLayer.path = UIBezierPath(
roundedRect: rescueNowButton.bounds,
byRoundingCorners: [.bottomLeft, .bottomRight],
cornerRadii: CGSize(width: MyTheme.cornerRadius, height: MyTheme.cornerRadius)).cgPath
rescueNowButton.layer.mask = maskLayer
}
I would like to suggest to use Layers to make your button round. 我建议使用“图层”使按钮变圆。 You can use button.layer.cornerRadius , .layer.borderwidth to get your button with rounded corner.
您可以使用button.layer.cornerRadius,.layer.borderwidth来获得带有圆角的按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.