Aker Abos
Aker Abos

Reputation: 21

Multiple rounded corners on view

I have a view which I want to set multiple rounded corners on. It´s a normal squared UIView.

And I´m calling this function which is an extension:

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
}

With the following:

myView.roundCorners(.bottomLeft, radius: 20)
myView.roundCorners(.bottomRight, radius: 20)

But only bottomRight is rounded, any clues why?

Upvotes: 0

Views: 405

Answers (2)

amber
amber

Reputation: 51

As Rashwan L said above UIRectCorner takes an array of options. What happened when you really called the method the mask is overrided. Also in the method after setting the mask self.layer.maskToBounds = true and before modifying self call: self.layoutIfNeeded() It prepares your views to be drawn correctly.

Upvotes: 0

Rashwan L
Rashwan L

Reputation: 38833

The UIRectCorner parameter is a struct that conforms to optionSet protocol which allows you to pass multiple members of the set. Each time you call that method with a different roundCorners mask it will override the previous layout.

You need to do this if you want multiple rounded corners for your view:

myView.roundCorners([.bottomLeft, .bottomRight], radius: 20)

Upvotes: 3

Related Questions