【问题标题】:Custom background drawing in an grouped UITableViewCell分组 UITableViewCell 中的自定义背景绘图
【发布时间】:2011-09-15 15:57:17
【问题描述】:

当样式设置为“分组”并且单元格是该部分的第一个或最后一个时,我在 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

【问题讨论】:

    标签: cocoa-touch uitableview quartz-graphics cagradientlayer


    【解决方案1】:

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

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

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

    【讨论】:

    • 太好了,这似乎是要走的路。谢谢!
    【解决方案2】:

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

    查看Rounded UIView using CALayers - only some corners - How? 了解更多示例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-22
      • 1970-01-01
      • 2010-09-21
      • 2011-12-15
      相关资源
      最近更新 更多