Reputation: 6065
I am trying to make rounded corners for my views and fencing a strange problem.
I use the following code to make rounded corners for my view
bezierPath = [UIBezierPath bezierPathWithRoundedRect:btnView.bounds
byRoundingCorners:UIRectCornerAllCorners
cornerRadii:radius];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = btnView.bounds;
maskLayer.path = bezierPath.CGPath;
btnView.layer.mask = maskLayer;
The problem is, it is not working for bottom corners. If I use UIRectCornerBottomLeft
nothing happens, if I use UIRectCornerAllCorners
only top corners are rounded.
EDIT: I dont use layer.cornerRadius because I just want to round bottom corners.
It turned out I do not have problem, if I remove UIViewAutoresizingFlexibleHeight
autoresizing mask from my view. But I would like to use autoresizing mask. Is it possible if yes how?
I have already tried setting autoresizingMask before and after setting the mask of the layer. No luck!
Upvotes: 12
Views: 9599
Reputation: 106
Another Way
Execution in " viewWillLayoutSubviews "
- (void)viewWillLayoutSubviews{
[self roundCornersOnView:self.buttonJoin onTopLeft:NO topRight:YES bottomLeft:YES bottomRight:NO radius:10.0];
}
Function
-(UIButton *)roundCornersOnView:(UIButton *)button onTopLeft:(BOOL)tl topRight:(BOOL)tr bottomLeft:(BOOL)bl bottomRight:(BOOL)br radius:(float)radius {
if (tl || tr || bl || br) {
UIRectCorner corner = 0; //holds the corner
//Determine which corner(s) should be changed
if (tl) {
corner = corner | UIRectCornerTopLeft;
}
if (tr) {
corner = corner | UIRectCornerTopRight;
}
if (bl) {
corner = corner | UIRectCornerBottomLeft;
}
if (br) {
corner = corner | UIRectCornerBottomRight;
}
UIButton *roundedView = button;
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:roundedView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = roundedView.bounds;
maskLayer.path = maskPath.CGPath;
roundedView.layer.mask = maskLayer;
return roundedView;
} else {
return button;
}
}
Upvotes: 0
Reputation: 2151
Finally i got the sucess and it really works. Try It.
CGRect frame = self.accountBackgroundImage2.frame;
frame.size.height = self.view.bounds.size.height;
UIBezierPath *path=[UIBezierPath bezierPathWithRoundedRect:self.accountBackgroundImage2.bounds byRoundingCorners:(UIRectCornerBottomLeft |UIRectCornerBottomRight) cornerRadii:CGSizeMake(12.5, 12.5)];
CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
shapeLayer.frame = self.accountBackgroundImage2.bounds;
shapeLayer.path = path.CGPath;
_accountBackgroundImage2.layer.mask = shapeLayer;
self.accountBackgroundImage2.frame = frame;
Upvotes: 0
Reputation: 111
Had to change corners of view. did not work with bottom corners in viewDidLoad due to autoresizing. Had to move code to viewDidAppear.
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
CGRect bounds = self.view.bounds;
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft) cornerRadii:CGSizeMake(5.0, 5.0)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = bounds;
maskLayer.path = maskPath.CGPath;
self.view.layer.mask = maskLayer;
self.view.layer.masksToBounds = YES;
[self.view setNeedsDisplay];
}
Upvotes: 0
Reputation: 7062
Solution:
-(void)roundCorners:(UIRectCorner)corner radius:(float)radius
{
_cornerRadius = radius;
_corner = corner;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
// Drawing code
[super drawRect:rect];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:_corner cornerRadii:CGSizeMake(_cornerRadius, _cornerRadius)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
}
Upvotes: 1
Reputation: 1677
I have just had a similar problem that's taken hours to solve - the answer was very simple: move my code from viewDidLoad to viewDidLayoutSubviews: this is the first method called after autolayout has completed .... so I suspect I did have 4 rounded corners but somehow they were off the edge of the screen.
Upvotes: 12
Reputation: 534
you can user view.layer.cornerRadius
to set the rounded corners.
Edited: Use the following
layer.mask = MTDContextCreateRoundedMask(layer.bounds, topleftRadius, topRightRadius, bottomLeftRadius,bottomRightRadius);
This is the supported method.
static inline UIImage* MTDContextCreateRoundedMask(CGRect rect, CGFloat radius_tl, CGFloat radius_tr, CGFloat radius_bl, CGFloat radius_br)
{
CGContextRef context;
CGColorSpaceRef colorSpace;
colorSpace = CGColorSpaceCreateDeviceRGB();
context = CGBitmapContextCreate( NULL, rect.size.width, rect.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast );
CGColorSpaceRelease(colorSpace);
if ( context == NULL ) {
return NULL;
}
CGFloat minx = CGRectGetMinX( rect ), midx = CGRectGetMidX( rect ), maxx = CGRectGetMaxX( rect );
CGFloat miny = CGRectGetMinY( rect ), midy = CGRectGetMidY( rect ), maxy = CGRectGetMaxY( rect );
CGContextBeginPath( context );
CGContextSetGrayFillColor( context, 1.0, 0.0 );
CGContextAddRect( context, rect );
CGContextClosePath( context );
CGContextDrawPath( context, kCGPathFill );
CGContextSetGrayFillColor( context, 1.0, 1.0 );
CGContextBeginPath( context );
CGContextMoveToPoint( context, minx, midy );
CGContextAddArcToPoint( context, minx, miny, midx, miny, radius_bl );
CGContextAddArcToPoint( context, maxx, miny, maxx, midy, radius_br );
CGContextAddArcToPoint( context, maxx, maxy, midx, maxy, radius_tr );
CGContextAddArcToPoint( context, minx, maxy, minx, midy, radius_tl );
CGContextClosePath( context );
CGContextDrawPath( context, kCGPathFill );
CGImageRef bitmapContext = CGBitmapContextCreateImage( context );
CGContextRelease( context );
UIImage *theImage = [UIImage imageWithCGImage:bitmapContext];
CGImageRelease(bitmapContext);
return theImage.layer.mask;
}
Upvotes: 0
Reputation: 6065
Until I get an another answer, if it is possible to use autoresizingMask with UIBezierPath
, I say NO and mark as correct answer.
I do not delete my question, because anybody can fence the same problem without knowing the reason.
Upvotes: 3
Reputation: 20541
then instead of that use bellow code for rounded corners...
btnView.layer.cornerRadius = 8.0;
just import QuartzCore/QuartzCore.h
in your .m
like bellow...
#import <QuartzCore/QuartzCore.h>
UPDATE:
For that you can use different RoundingCorners
Properties of UIBezierPath
lik bellow....
For Ex : UIRectCornerBottomLeft
,UIRectCornerBottomRight
,UIRectCornerTopLeft
,UIRectCornerTopRight
and UIRectCornerAllCorners
.
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect: btnView.bounds
byRoundingCorners:UIRectCornerBottomLeft
cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = btnView.bounds;
maskLayer.path = maskPath.CGPath;
btnView.layer.mask = maskLayer;
[btnView setNeedsDisplay];
Upvotes: 1