1

I am working in Swift 4 and I am trying to make a button with just two of the four corners rounded (for some buttons the top-right and button right and for some the top-left and bottom-left corners).

I've search a lot and all that I've found is this: myButton.layer.cornerRadius = newCornerRadiusInPixels;. It works perfectly but it roundes all the corners at the same time.

is there a way to round just a particular corner? Thanks!

2 Answers2

2

Available in iOS 11 and up:

myButton.layer.maskedCorners = [.layerMaxXMaxYCorner]  // or some other corner(s)
Tim Vermeulen
  • 12,352
  • 9
  • 44
  • 63
  • This is correct. Available variables for all 4 corners: [.layerMaxXMinYCorner, .layerMinXMinYCorner, .layerMinXMaxYCorner, .layerMaxXMaxYCorner] – marika.daboja Aug 10 '23 at 05:56
0

Try this one

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
    }

}

view.roundCorners([.topLeft, .bottomRight], radius: 10)
Nikunj Kumbhani
  • 3,758
  • 2
  • 26
  • 51
  • thanks man, it did not work for me. I used exactly as shown and placed it in the view did load. Am I missing something? –  Aug 10 '18 at 19:22