我正在尝试使用下面的代码来绕顶角 func roundCorners(corners:UIRectCorner, radius: CGFloat) { let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) le
func roundCorners(corners:UIRectCorner, radius: CGFloat) { let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) let maskLayer = CAShapeLayer() maskLayer.frame = self.bounds maskLayer.path = path.cgPath self.layer.mask = maskLayer }
使用myView.roundCorners(corner:[.topLeft,.topRight],radius:radius)
但它正在围绕观点的一边:
这是在tableView sectionHeader中,如果我向下滚动然后使用相同的代码向上舍入:
顶部折扣视角也使用相同的功能进行四舍五入.
感谢帮助.
更新
如果我修复视图上的宽度,那么它工作正常.
Swift 4.2:
extension UIView { func roundCorners(_ corners: UIRectCorner, radius: CGFloat) { if #available(iOS 11.0, *) { clipsToBounds = true layer.cornerRadius = radius layer.maskedCorners = CACornerMask(rawValue: corners.rawValue) } else { let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) let mask = CAShapeLayer() mask.path = path.cgPath layer.mask = mask } } }