2011-12-24 133 views
2

我想添加圆角只有我的UITableView的顶部角落,但问题是,与下面的代码,它只是在我的整个UITableView黑色层。我将如何解决这个问题?将圆角添加到UITableView的顶部?

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

回答

-1

我觉得做为其查看有圆形的顶部corner.Its只是一个方案来解决problem.May是它会帮助你的表视图背景视图的最佳途径。

+0

这将是同样的问题。我的问题通常是任何UIView或其子类。所以我需要弄清楚这一点。 – 2011-12-25 06:53:30

+2

http://stackoverflow.com/questions/2264083/rounded-uiview-using-calayers-only-some-corners-how检查出来可能会帮助你。 – Emon 2011-12-25 07:17:57

+0

哦,而不是做addSublayer,我应该只是设置CCLayer的mask属性。那么如何在一个掩码中添加多个UIRextconrners? – 2011-12-25 07:31:56

6

下面的代码对我的作品的导航栏,只是改变了第一线,把你的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;