9

I created a rectangle using following code and now I need to rounded the corners of this rectangle. but I can't find a property called layer.cornerRadius, can anyone help me ?

class OvalLayer: CAShapeLayer {

    let animationDuration: CFTimeInterval = 0.3

    override init() {
        super.init()
        fillColor = Colors.green.CGColor
        path = ovalPathSmall.CGPath
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    var ovalPathStart: UIBezierPath {
        let path = UIBezierPath(ovalInRect: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0))

        return path
    }
}
Miknash
  • 7,888
  • 3
  • 34
  • 46
roledene JKS
  • 405
  • 2
  • 8
  • 12
  • u can use UIBezierPath itself while drawing to make corner radius – vaibby Jan 28 '16 at 05:59
  • Since you are using **UIBezierPath** you need to draw rounded corner using UIBezierPath only. Check [this](http://ronnqvi.st/thinking-like-a-bzier-path/) out, it will help you what's needed. – iphonic Jan 28 '16 at 05:59
  • @iphonic Can you please send me the link which you have suggested. I won't be able to open this. – Khushbu Desai May 28 '18 at 04:33
  • 1
    @KhushbuDesai It seems they have moved it, I found a new link [here](http://ronnqvi.st/thinking-like-a-bzier-path) it is... – iphonic May 28 '18 at 04:49
  • @iphonic can you please help me with this. https://stackoverflow.com/questions/50541219/how-to-set-corner-radius-to-uibezierpath/50541871?noredirect=1#comment88096434_50541871 – Khushbu Desai May 28 '18 at 04:52

6 Answers6

19

You can use below method to make all corner round of view...

 UIBezierPath(roundedRect: anyView.bounds, cornerRadius: CGSize(width: 10.0, height: 10.0))

If you want particular corner to make round use below method.

 UIBezierPath(roundedRect: anyView.bounds,
        byRoundingCorners: .BottomLeft | .BottomRight,
        cornerRadius: CGSize(width: 10.0, height: 10.0))
Bhoomi Jagani
  • 2,413
  • 18
  • 24
7

use like this:

let pathWithRadius = UIBezierPath(roundedRect:yourView.bounds, byRoundingCorners:[.TopRight, .TopLeft], cornerRadii: CGSizeMake(5.0, 5.0))
let maskLayer = CAShapeLayer()
maskLayer.pathWithRadius = pathWithRadius.CGPath
yourView.layer.mask = maskLayer

for All Corner Radius edit this

[.TopRight,.TopLeft,.BottomRight, .BottomLeft]
Vvk
  • 4,031
  • 29
  • 51
2

Objective c:

UIBezierPath* path = [UIBezierPath
    bezierPathWithRoundedRect: CGRectMake(0, 0, 150, 153)
                 cornerRadius: 50];

SWIFT :

var path: UIBezierPath = UIBezierPath(roundedRect: CGRectMake(0, 0, 150, 153), cornerRadius: 50)

Hope this will help.

Mayank Patel
  • 3,868
  • 10
  • 36
  • 59
KDeogharkar
  • 10,939
  • 7
  • 51
  • 95
2

use this initializer

let path1 = UIBezierPath(roundedRect: CGRect, cornerRadius: CGFloat)

or

let path1 = UIBezierPath(roundedRect:  CGRect, byRoundingCorners: UIRectCorner, cornerRadii: CGSize))
Ajay Kumar
  • 1,807
  • 18
  • 27
0

As simple as:

 UIBezierPath(roundedRect: anyView.bounds, cornerRadius: CGFloat(4.0))
oskarko
  • 3,382
  • 1
  • 26
  • 26
0

If you want to rounded the corners of a rectangle, you can use

UIBezierPath(roundedRect:  CGRect, byRoundingCorners: UIRectCorner,
cornerRadii: CGSize)

But if you want to do them oval, you can't do it. That's because UIBezierPath ignore cornerRadii.height. I fixed it in my implementation.

https://github.com/BuTaJIuK-ua/UIBezierPath-Extension