2011-12-24 112 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;