0

Rounded corner

I know how to round the all four corners by setting the corner radius similar to this:

 func rounded() {
        self.layer.cornerRadius = self.frame.size.height / 2
    }

but am unsure how to round only 2 corners. Any help is appreciated.

enter image description hereI'm trying to round the corners of only one side of a UIView like this:

SwiftyJD
  • 5,257
  • 7
  • 41
  • 92

1 Answers1

1

You can try

extension UIView {
    func roundedLeftTopBottom(){
        self.clipsToBounds = true
        let maskPath1 = UIBezierPath(roundedRect: bounds,
                                     byRoundingCorners: [.topLeft , .bottomLeft],
                                     cornerRadii: CGSize(width:self.frame.size.height / 2, height:self.frame.size.height / 2))
        let maskLayer1 = CAShapeLayer()
        maskLayer1.frame = bounds
        maskLayer1.path = maskPath1.cgPath
        layer.mask = maskLayer1
    }

    // or

    func round () {
        self.layer.cornerRadius = self.frame.size.height / 2
        self.layer.maskedCorners = [.layerMinXMinYCorner, .layerMinXMaxYCorner]
    }
}

Make sure to call any of those ones inside layoutSubviews or viewDidLayoutSubviews if the view is inside a vc so to have the right frame size

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87