The real world doesn’t have straight edges. Things are rounded. Our UIViews can be too! Most of us know about the
cornerRadius property on CALayer. We can set a value, then see all the corners round themselves to that value:
contentView.layer.cornerRadius = 8
Nice, but what if we only want to round some of the corners though?
let cornerRadius: CGFloat = 8 let maskLayer = CAShapeLayer() maskLayer.path = UIBezierPath( roundedRect: view.bounds, byRoundingCorners: [.BottomLeft, .BottomRight], cornerRadii: CGSize(width: cornerRadius, height: cornerRadius) ).CGPath view.layer.mask = maskLayer
For this, we’ll need to get a bit more creative. We can use an often-overlooked type called UIRectCorner.
It lets us describe which corners we’d like to round. Then we’ll use a UIBezierPath to create a “mask” layer that will only allow some of the content to “show through”.
We pass in an option set for the
byRoundingCorners parameter, listing out the corners we’d like rounded.
Success! Happy rounding!
Update: Shout-out to Reddit-reader /u/cuomo456 who reminds us to update/replace our layer masks anytime the view/layer we’re masking’s frame or bounds changes. If we don’t, we could find ourselves scratching our heads why our rounded corners aren’t working in a UITableViewCell, for example.