2

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?

Titouan de Bailleul
  • 12,920
  • 11
  • 66
  • 121
  • see this [question](http://stackoverflow.com/questions/2264083/rounded-uiview-using-calayers-only-some-corners-how) - 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 Jul 23 '12 at 13:23
  • you should use a unique mask for your view to reach this effect. – holex Jul 23 '12 at 13:49

2 Answers2

3

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)];
lawicko
  • 7,246
  • 3
  • 37
  • 49
0

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;
Denis Kulygin
  • 323
  • 1
  • 9