1

UITableViewCell当样式设置为“分组”并且单元格是该部分的第一个或最后一个时,我在绘制自定义渐变背景时遇到了一些麻烦。我的方法是简单地创建一个CAGradientLayer并将其添加到视图中,如下所示:

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = rect;
gradient.colors = [NSArray arrayWithObjects:(id)[_backgroundColorLight CGColor], (id)[_backgroundColorDark CGColor], nil];
[self.backgroundView.layer insertSublayer:gradient atIndex:0];
self.backgroundView.layer.masksToBounds = YES;

不幸的是,这会产生像这样的细胞:背景超出边缘

有没有人提示如何使背景适合单元格的边界?

谢谢
-f

4

2 回答 2

2

如果您绘制自定义背景,您还必须自己绘制边框。那里有很多开源的东西。

基本上,你需要知道[在单元格中]它是否是顶部/中间/底部/单个,并在 drawRect 中剪切东西。insertSublayer 不会让您走得太远。

查看您在此处找到的 PageCellBackground 类:http: //cocoawithlove.com/2010/12/uitableview-construction-drawing-and.html

于 2011-09-16T07:19:26.457 回答
1

我没有尝试过,但 CAGradientLayer 恰好是 CALayer 的子类,所以也许设置它的borderRadius 可能会起作用。但是它会圆所有角,因此您可以通过使图层大于单元格并让单元格视图将其切断来弥补这一点。

使用 CALayers查看Rounded UIView - 只有一些角落 - 如何?更多示例

于 2011-09-16T08:07:28.273 回答