Create a rectangle with just two rounded corners in swift?

后端 未结 15 3048
南笙
南笙 2020-11-28 03:14

I need to create a rectangle that have just two rounded corners in swift (Objective C code also ok).

At the moment my code is creating two rectangles with



        
15条回答
  •  余生分开走
    2020-11-28 03:38

    Swift 3 - Useful UIView extension when you need to round specific corners of some views:

    extension UIView {
      func round(corners: UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
      }
    }
    

    then just use it like this:

    someView.round(corners: [.topLeft, .topRight], radius: 5)
    

提交回复
热议问题