how to set cornerRadius for only bottom-left,bottom-right and top-left corner of a UIView?

前端 未结 10 1171
Happy的楠姐
Happy的楠姐 2020-12-04 06:49

Is there a way to set cornerRadius for only bottom-left,bottom-right and top-left corner of a UIView?

I tried the following, but it ended up making the view disappea

10条回答
  •  遥遥无期
    2020-12-04 07:32

    Tested in xcode 8 and swift 3

    extension UIView {
        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
        }
    }
    

    And use like this

    YourView.roundCorners([.topLeft, .bottomLeft], radius: 10)
    

提交回复
热议问题