Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set cornerRadius for only bottom-left,bottom-right and top-left corner of a UIView?

Is there a way to set cornerRadius for only bottom-left,bottom-right and top-left corner of a UIView?

I tried the following, but it ended up making the view disappear. Is there anything wrong with the code below?

    UIBezierPath *maskPath;     maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(20.0, 20.0)];     CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];     maskLayer.path = maskPath.CGPath;     view.layer.mask = maskLayer; 
like image 964
Siva Avatar asked Sep 02 '14 05:09

Siva


People also ask

How do you change the corner radius of a storyboard?

Select the view that you want to round and open its Identity Inspector. In the User Defined Runtime Attributes section, add the following two entries: Key Path: layer. cornerRadius , Type: Number, Value: (whatever radius you want)

How do you label corner radius in Swift?

"label. layer. masksToBounds = true" is an important code, if you apply corner radius to a label and if it dosen't work than adding this will definitely add the corner radius to a particular label.. So this should be the correct answer..


1 Answers

You can do it like this:

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.viewOutlet.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)];  CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.view.bounds; maskLayer.path  = maskPath.CGPath; self.viewOutlet.layer.mask = maskLayer; 

enter image description here

Update:
If You need border just create another CAShapeLayer and add it to view's layer as sublayer. Like this (place this code below upper code):

CAShapeLayer *borderLayer = [[CAShapeLayer alloc] init]; borderLayer.frame = self.view.bounds; borderLayer.path  = maskPath.CGPath; borderLayer.lineWidth   = 4.0f; borderLayer.strokeColor = [UIColor blackColor].CGColor; borderLayer.fillColor   = [UIColor clearColor].CGColor;  [self.viewOutlet.layer addSublayer:borderLayer]; 

enter image description here

In swift 3.0 like this:

let maskPath = UIBezierPath.init(roundedRect: self.viewOutlet.bounds, byRoundingCorners:[.topLeft, .bottomLeft], cornerRadii: CGSize.init(width: 10.0, height: 10.0)) let maskLayer = CAShapeLayer() maskLayer.frame = self.viewOutlet.bounds maskLayer.path = maskPath.cgPath self.viewOutlet.layer.mask = maskLayer 
like image 108
Justin Boo Avatar answered Oct 06 '22 00:10

Justin Boo