Adding corners and shadows to UIView in iOS (Swift)

February 06, 2019

In a lot of design philosophies, you may notice that a lot of them like rounded corners as well as adding a dropshow onto the view. In iOS, there's no obvious way to achieve the functionality. This snippet will show you two ways of being able to do so.

Method 1:

// corner radius
myview.layer.cornerRadius = 10

// border
myview.layer.borderWidth = 1.0
myview.layer.borderColor = UIColor.black.cgColor

// shadow
myview.layer.shadowColor = UIColor.black.cgColor
myview.layer.shadowOffset = CGSize(width: 3, height: 3)
myview.layer.shadowOpacity = 0.7
myview.layer.shadowRadius = 4.0

Method 2: (For performance!)

myview.layer.shadowPath = UIBezierPath(roundedRect: baseView.bounds, cornerRadius: 10).cgPath
myview.layer.shouldRasterize = true
myview.layer.rasterizationScale = UIScreen.main.scale