How to give cornerRadius for UIBezierPath

11,516

Solution 1

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

Solution 2

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]

Solution 3

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.

Solution 4

use this initializer

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

or

let path1 = UIBezierPath(roundedRect:  CGRect, byRoundingCorners: UIRectCorner, cornerRadii: CGSize))
Share:
11,516
roledene JKS
Author by

roledene JKS

Updated on June 15, 2022

Comments

  • roledene JKS
    roledene JKS almost 2 years

    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
        }
    }
    
  • Lakshmi Keerthana Siddu
    Lakshmi Keerthana Siddu about 7 years
    can you please suggest rounded corners for custom shapes like star.
  • Khushbu Desai
    Khushbu Desai almost 6 years
    @LakshmiKeerthanaSiddu have you done rounded corners for star? I want to do the same. Can you please help me!
  • Lakshmi Keerthana Siddu
    Lakshmi Keerthana Siddu almost 6 years
    yes, I did but not with uibezierpath . i got with CGPath or otherwise you can use paint tool