0

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

1

Available in iOS 11 and up:

myButton.layer.maskedCorners = [.layerMaxXMaxYCorner]  // or some other corner(s)
Tim Vermeulen
  • 12,063
  • 9
  • 43
  • 63
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,432
  • 1
  • 21
  • 45
  • 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