jazzedge
11/28/2017 - 5:47 PM

Swift - Extension to create a curved edge UIView

This extension allows you to call one of two functions and set one or more corners of a UIView to curve. If you set, for example, the bottomLeft and bottomRight corners, your UIView will have an outward curved base.

See: https://stackoverflow.com/questions/31232689/how-to-set-cornerradius-for-only-bottom-left-bottom-right-and-top-left-corner-te/41217791#41217791

Source: CurvedView

EXTENSION:

import UIKit

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
    }
}

VIEWCONTROLLER:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var curvyView: UIView!
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        // Do any additional setup after loading the view, typically from a nib.
        curvyView.roundCorners([.bottomLeft, .bottomRight], radius: curvyView.frame.width/4)
    }
}