我试图通过在layoutSubviews
中为UITableViewCell
调用此方法来围绕按钮的底角:
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
}
但它根本不起作用,角落不圆。我在这里做错了什么?
答案 0 :(得分:0)
您可以定义扩展程序:
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
}
}
你也可以为其他角落做同样的事情。只需更改byRoundingCorners [.BottomLeft , .BottomRight]
答案 1 :(得分:0)
您的代码存在的问题是您从未设置过遮罩的框架。此外,没有必要设置clipsToBounds
和layer.masksToBounds
:
可以删除这些行:
rescueNowButton.clipsToBounds = true
rescueNowButton.layer.masksToBounds = true
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
}
答案 2 :(得分:-1)
我建议使用图层来制作按钮。 你可以使用button.layer.cornerRadius,.layer.borderwidth来获得带圆角的按钮。