Search code examples
swiftshadowcornerradius

How to add selected corner radius and shadow for UIView in swift


Can someone please help me with this question in iOS Swift. I have an UI view with drop shadow effect. I need corner radius for 3 corners except for one corner. Any help is appreciated.

Thanks, Dharani


Solution

  • You can use extension on UIView:

    Here is a sample I created on Playgrounds:

    let view = UIView()
    
    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
        }
    }
    // customize where you want your roundCorners on your UIView
    view.roundCorners([.topLeft, .topRight, .bottomRight], radius: 10)