Round Top Corners of a UIView in Swift

后端 未结 2 1343
死守一世寂寞
死守一世寂寞 2020-12-31 20:30

I am trying to round top corners using below code

func roundCorners(corners:UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: se         


        
2条回答
  •  隐瞒了意图╮
    2020-12-31 20:41

    Solved this with the help of @Paolo and below is the working code.

    Swift 3.2

    extension UIView {
    
        func roundCorners(corners:UIRectCorner, radius: CGFloat) {
    
            DispatchQueue.main.async {
                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
            }
        }
    }
    

    for calling this function use below line and mention which corners you want to round

    self.myView.roundCorners(corners: [.topLeft, .topRight, .bottomLeft, .bottomRight], radius: 8.0)
    

提交回复
热议问题