Search code examples
iosswiftuiviewuibezierpath

Only top left corner is rounded instead of topLeft and bottomLeft


I am trying to make UIView topLeft and bottomLeft corners rounded however for some reason only topLeft has border radius. What can cause this?

This is What I am doing:

I have Extension:

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
    }
}

And I apply it to view like this:

myView.roundCorners([.topLeft, .bottomLeft], radius: 10.0)

I am using it inside cell and I am also using auto-layout.

At the moment it looks like this(green is the view I am talking about) but I want the bottomLeft to have corner radius also:

enter image description here


Solution

  • The problem is your timing. Your extension relies on self.bounds. But if you call this extension too early, such as viewDidLoad, those bounds have not yet been determined. Your view is sized by auto layout; that means you don't have bounds until your view's layoutSubviews or your view controller's viewDidLayoutSubviews has been called.