2

I am trying to add rounded corners to only the top corners of my UITableView but the problem is, with the code below, it just makes a black layer over my whole UITableView. How would I fix this?

//Rounded Corners for top corners of UITableView
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:thetableView.bounds 
                                               byRoundingCorners:UIRectCornerTopLeft
                                                     cornerRadii:CGSizeMake(10.0, 10.0)];
UIBezierPath *maskPath2 = [UIBezierPath bezierPathWithRoundedRect:thetableView.bounds 
                                               byRoundingCorners:UIRectCornerTopRight
                                                     cornerRadii:CGSizeMake(10.0, 10.0)];
// Create the shape layer and set its path
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = thetableView.bounds;
maskLayer.path = maskPath.CGPath;

CAShapeLayer *maskLayer2 = [CAShapeLayer layer];
maskLayer.frame = thetableView.bounds;
maskLayer.path = maskPath2.CGPath;

// Set the newly created shape layer as the mask for the image view's layer
[thetableView.layer addSublayer:maskLayer];
[thetableView.layer addSublayer:maskLayer2];
SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191

2 Answers2

6

the following code works for me for a navigationbar, just change the first line and put your self.tableView:

CALayer *capa = [self.navigationController navigationBar].layer;
[capa setShadowColor: [[UIColor blackColor] CGColor]];
[capa setShadowOpacity:0.85f];
[capa setShadowOffset: CGSizeMake(0.0f, 1.5f)];
[capa setShadowRadius:2.0f];  
[capa setShouldRasterize:YES];


//Round
CGRect bounds = capa.bounds;
bounds.size.height += 10.0f;    //I'm reserving enough room for the shadow
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds 
                                               byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
                                                     cornerRadii:CGSizeMake(10.0, 10.0)];

CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = bounds;
maskLayer.path = maskPath.CGPath;

[capa addSublayer:maskLayer];
capa.mask = maskLayer;
Joze
  • 668
  • 7
  • 13
-1

I think the best way to make a background view for the table view which view has rounded top corner.Its just a solution to solve the problem.May be it will help you.

Emon
  • 958
  • 3
  • 10
  • 28
  • This would be the same issue. My issue is in general with any UIView or its subclasses. So I kind of need to figure this out. – SimplyKiwi Dec 25 '11 at 06:53
  • 2
    http://stackoverflow.com/questions/2264083/rounded-uiview-using-calayers-only-some-corners-how check it out may be it will help you. – Emon Dec 25 '11 at 07:17
  • oh, instead of doing addSublayer, I should be just setting the mask property of the CCLayer. So how would I add multiple UIRextconrners in one mask? – SimplyKiwi Dec 25 '11 at 07:31
  • I do not know wtf is `UIRextconrners`? It will very much helpful for me if you give me an example of your requirement...it can be image. – Emon Dec 25 '11 at 08:27
  • Sorry I meant UIRectCorners. I am simply just trying to apply a corners radius to a view but only to the top corners and NOT the bottom 2. But my problem is now with that code is that it covers the view with a black layer! – SimplyKiwi Dec 25 '11 at 17:00