【发布时间】:2016-09-06 22:14:03
【问题描述】:
我知道我可以使用以下方法绕过所有四个角:
myBtn.layer.cornerRadius = 8
myBtn.layer.masksToBounds = true
由于我只想第二轮,所以我做了一些研究,发现this:
extension UIView {
func roundCorners(corners:UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.CGPath
self.layer.mask = mask
}
}
这样称呼:
view.roundCorners([.TopLeft , .TopRight], radius: 10)
但这不适用于 UIButton。当我将扩展名切换为 UIButton 类型并将其传递给 button 时,输出如下所示:
问题是,如何调整它以在 UIButton 上工作?
【问题讨论】:
-
请考虑接受我的回答,因为这是对问题最准确的回答。这个问题的未来访问者不会仅仅因为看到一堆有效的代码而感到满意,但并没有解释它为什么有效(这就是当前接受的答案所做的),而是通过看到 where 问题所在– 我的回答已经确定了这一点。
-
在这里查看我的答案:stackoverflow.com/a/40222533/2594699
标签: ios swift uibutton rounded-corners