Rounded Corners only on Top of a UIView

后端 未结 10 1965
别跟我提以往
别跟我提以往 2020-11-29 22:53

Hi i am searching a clean solution without overwriting drawRect or stuff like that to create a UIView with Rounded corners on the Top of the

10条回答
  •  情书的邮戳
    2020-11-29 23:22

    An extension for UIView that rounds selected corners (Swift 4):

    extension UIView {
    
        /// Round UIView selected corners
        ///
        /// - Parameters:
        ///   - corners: selected corners to round
        ///   - radius: round amount
        func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
            let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
            let mask = CAShapeLayer()
            mask.path = path.cgPath
            self.layer.mask = mask
        }
    }
    

    example:

    ratingView.roundCorners([.topLeft, .topRight, .bottomRight], radius: 6)
    

提交回复
热议问题