使用示例:
let bgView = UIView()
bgView.frame = CGRect(x: 0.5*(UIScreen.main.bounds.size.width-100), y: 100, width: 100, height: 100)
bgView.backgroundColor = .orange
view.addSubview(bgView)
bgView.addCorner(conrners: [.topLeft,.topRight], radius: 10)
UIView扩展圆角方法:
extension UIView {
///添加部分圆角(自动布局情况下,要在布局后使用)
func addCorner(conrners: UIRectCorner , radius: CGFloat) {
self.layoutIfNeeded()
let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: conrners, cornerRadii: CGSize(width: radius, height: radius))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.bounds
maskLayer.path = maskPath.cgPath
self.layer.mask = maskLayer
}
}
网友评论