原地址:https://www.jianshu.com/p/2b939f53debf
extension UIView{
public func extSetPartCornerRadiusHan(byRoundingCorners corners: UIRectCorner, radii: CGFloat) {
let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize.init(width: radii, height: radii))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.bounds
maskLayer.path = maskPath.cgPath
self.layer.mask = maskLayer
}
}
注意: 添加圆角的View如果是用约束做的布局,那么在VC里面在viewDidLayoutSubviews函数下调用
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.firstView.extSetPartCornerRadiusHan(byRoundingCorners: [UIRectCorner.bottomLeft,UIRectCorner.bottomRight], radii: 10)
}
在View里面添加在这个函数下面
override func layoutSubviews() {
super.layoutSubviews()
}
原因: 用约束去做布局,视图的frame 和 bounds 不会立刻绘制,所以 width height 是0 看不到效果