Reputation: 2316
I want to set the Top/Bottom Left corner radius of UIButton. How can i achieve this?
If i use button.layer.cornerRadius
, it'll set all the corners.
I've tried using UIBazierPath, but probably i'm doing something wrong and the button is not appearing correctly. The code i've used is:
let path = UIBezierPath(roundedRect: button.bounds, byRoundingCorners: [.TopLeft, .BottomLeft], cornerRadii: CGSize(width: 3.0, height: 3.0))
let mask = CAShapeLayer()
mask.path = path.CGPath
button.layer.mask = mask
I've tried the answers from stackoverflow i.e this but it's not working for me. It's updating the corners but reducing button's height as well. I think i'm doing something wrong with frame/bounds.
Using above code, the UIButton appears as:
Another solution i used was to use the image with rounded corners. but now i want to do this programmatically.
Thanks.
Upvotes: 0
Views: 860
Reputation: 1055
Did your layout change? For instance, if you place the code in viewDidLoad
of a UIViewController
and the view gets resized by autolayout, then this could happen. If you move the code to viewDidLayoutSubviews
then it should work.
Upvotes: 2
Reputation: 681
I have achieved this in objective C by using the following method:
-(void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
{
UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds
byRoundingCorners:corners
cornerRadii:CGSizeMake(8.0, 8.0)];
CAShapeLayer* shape = [[CAShapeLayer alloc] init];
[shape setPath:rounded.CGPath];
view.layer.mask = shape;
}
Call the above method this way:
[self setMaskTo:self.myButton byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopLeft];
Upvotes: 1