保持UITableViewCell的背景视图忽略单元格边界

时间:2011-11-21 20:41:09

标签: iphone ios

我们在这里有一个非常自定义的UITableViewCell。所以它基本上是这样的:

+----------------+
+   Image view   +
+ (230px height) +
+----------------+
+ Content        +
+ Content        +
+ Overall 340px  +
+----------------+

我想要做的是:将背景图像添加到从图像结束处开始的单元格,并结束表格单元格结束的位置(无背景重复或任何内容)。

所以这就是我所做的:

    // Custom UITableViewCell class, this is part of a method that gets called
    // in an overriden -layoutSubviews
    UIImage *watermarkImage = [UIImage imageNamed:@"myBackground.png"];

    int watermarkWidth = watermarkImage.size.width;
    int watermarkHeight = watermarkImage.size.height;

    int watermarkX = screenWidth - watermarkWidth;
    int watermarkY = imageView.frame.size.height + 10;

    CGRect watermarkFrame = CGRectMake(watermarkX, watermarkY, watermarkWidth, watermarkHeight);

    UIImageView *watermarkImageView = [[UIImageView alloc] initWithFrame:watermarkFrame];
    [watermarkImageView setImage:watermarkImage];

    [self setBackgroundView:watermarkImageView];

这非常有效,除了图像大于CGRect因此大于细胞本身。这会导致backgroundView与下面的单元格重叠,说实话,这看起来很难看。

如何使backgroundView尊重细胞边界?将clipsToBounds设置为YES无效。

1 个答案:

答案 0 :(得分:0)

就像我在评论中发布的那样:我会在self上启用裁剪,因为子视图backgroundView太大了。

另外:我会用

UIImageView *imgView = [[UImageView alloc] initWithImage:]
imgView.frame = CGRectMake(...)

也尝试机器人设置框架,因为Cell也可以自动设置backgroundView

的框架