Norway


Topics

Topics

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

- 211 rounding corners with uirectcorner and uibezierpath 1 - #211: Rounding Corners with UIRectCorner and UIBezierPath πŸ–Œ – Little Bites of Cocoa

Nice, but what if we only want to round some of the corners though?

let cornerRadius: CGFloat = 8
let maskLayer = CAShapeLayer()

maskLayer.path = (
  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 .

It lets us describe which corners we’d like to round. Then we’ll use a UIBezierPath to 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.

- 211 rounding corners with uirectcorner and uibezierpath 2 - #211: Rounding Corners with UIRectCorner and UIBezierPath πŸ–Œ – Little Bites of Cocoa

Success! Happy !

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.



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here