在实际开发中,经常需要对UIView的左上角和右上角进行圆角设置,常见效果图如下:
这时候就需要我们通过代码来实现圆角。代码如下:
import UIKit
extension UIView {
/// 设置圆角
/// - Parameters:
/// - cornerRadii: 圆角幅度
/// - roundingCorners: UIRectCorner(rawValue: (UIRectCorner.topRight.rawValue) | (UIRectCorner.bottomRight.rawValue))
public func lzkj_filletedCorner(_ cornerRadii:CGSize,_ roundingCorners:UIRectCorner) {
let fieldPath = UIBezierPath.init(roundedRect: bounds, byRoundingCorners: roundingCorners, cornerRadii:cornerRadii )
let fieldLayer = CAShapeLayer()
fieldLayer.frame = bounds
fieldLayer.path = fieldPath.cgPath
self.layer.mask = fieldLayer
}
}
调用示例:
self.bottomTabView.lzkj_filletedCorner(CGSize(width: 10, height: 10),UIRectCorner(rawValue: (UIRectCorner.topLeft.rawValue)|(UIRectCorner.topRight.rawValue)))
IOS11之后,系统支持新的方法:
if #available(iOS 11.0, *) {
// iOS11:需要带用这个系统方法就可以随意设置View的圆角
custuView.layer.cornerRadius = 21
custuView.layer.maskedCorners = CACornerMask(rawValue: CACornerMask.layerMinXMinYCorner.rawValue | CACornerMask.layerMaxXMinYCorner.rawValue)
}