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 disappea
I know it's very late, but i just create one method where you can pass only one or multiple UIRectCorners.
[self setMaskTo:myView byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)];
- (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;
}
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:someView.bounds
byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomLeft|UIRectCornerBottomRight)
cornerRadii:CGSizeMake(10.0,10.0,5.0, 10.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
someView.layer.mask = maskLayer;
Setting corner-radius for a view in Swift
The creation and masking of layers should run inside the optional drawRect()
block inherited from UIView
override func drawRect(rect: CGRect) {
super.drawRect(rect)
let maskPath = UIBezierPath(roundedRect: self.view.bounds, byRoundingCorners: [.TopLeft, .BottomLeft, .BottomRight], cornerRadii: CGSizeMake(10, 10))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.view.bounds
maskLayer.path = maskPath.CGPath
self.view.layer.mask = maskLayer
}
Try this code
-(void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners {
UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer* shape = [[CAShapeLayer alloc] init];
[shape setPath:rounded.CGPath];
view.layer.mask = shape;
}
call using
[self setMaskTo:myView byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
Available options:
UIRectCornerTopLeft, UIRectCornerTopRight,UIRectCornerBottomLeft,UIRectCornerBottomRight,UIRectCornerAllCorners
Use a custom UIView
and Implement the below code
#import "CustomUIView.h"
@implementation CustomView
- (void)drawRect:(CGRect)rect
{
// Drawing code
CAShapeLayer * maskLayer = [CAShapeLayer layer];
maskLayer.path = [UIBezierPath bezierPathWithRoundedRect: self.bounds byRoundingCorners: UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii: (CGSize){20.0, 20.0}].CGPath;
self.layer.mask = maskLayer;
}
@end
Output:
Look at the UIBezierPath.h
options :
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0UL
};
hope this will help you
your above code running perfectly on my machine , may be you set CAShapeLayer
frame equal to your view frame due to which your view will disappear but i am not seeing this line in your code ,so please check your view property and apply below code
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:viewName.bounds
byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight |UIRectCornerTopLeft)
cornerRadii:CGSizeMake(20.0, 20.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame =viewName.bounds;
maskLayer.path = maskPath.CGPath;
viewName.layer.mask = maskLayer;