2
votes

I used the following method to get rounded corners on a view in my application:

(objective-c:)
hoursTable.layer.cornerRadius = 5.0;
[hoursTable setClipsToBounds:YES];

(my actual code is in rubymotion:)
self.view.layer.cornerRadius = 10
self.view.layer.masksToBounds = true

Now all the corners of that view are rounded with 10pt, how do I only apply the effect on the top-left and top-right corners and leave the bottom left and bottom right corner squared?

2
see this question - basically you need to mask the view's layer, and you can create a bezier path to define the mask which only has some corners rounded - wattson12
you should use a unique mask for your view to reach this effect. - holex

2 Answers

3
votes

I have the category on UIView with this code:

- (void)setRoundedCorners:(UIRectCorner)corners radius:(CGSize)size {
UIBezierPath* maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:size];

CAShapeLayer* maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;

self.layer.mask = maskLayer;
[maskLayer release];
}

Remove the last line if you use ARC.

Example of usage:

[hoursTable setRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight radius:CGSizeMake(5.0, 5.0)];
0
votes

Try to create path manually

CAShapeLayer* shape = [CAShapeLayer layer];
CGMutablePathRef path = CGPathCreateMutable();
/*...*/
CGPathMoveToPoint(path, NULL, 0, 0);
/*create path for view*/

shape.path = path;
CGPathRelease(path);
view.layer.mask = shape;