1.
extension UIView{ func addCorner(conrners: UIRectCorner , radius: CGFloat) { 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 } }
2. 使用。 controller 在
override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() //指定位置切圆角 self.bgview.jy.addCorner(conrners: [.topLeft , .topRight], radius: 6) }
自定义view 在 draw方法/layoutsubviews里面写 这里面可以获取到frame 进行圆角添加
override func draw(_ rect: CGRect) { super.draw(rect)
self.bgview.jy.addCorner(conrners: [.topLeft , .topRight], radius: 6)
}
- (void)layoutSubviews { [super layoutSubviews];
self.bgview.jy.addCorner(conrners: [.topLeft , .topRight], radius: 6)
}