Search code examples
iosuitableviewcalayer

Adding Rounded Corners to only top of UITableView?


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];

Solution

  • 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.