我们这里有一个非常定制的 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 无效。
最佳答案
就像我在评论中发表的那样:我会在 self
上启用剪辑,因为 subview backgroundView
太大了。
另外:我会用
UIImageView *imgView = [[UImageView alloc] initWithImage:]
imgView.frame = CGRectMake(...)
也尝试设置框架,因为 Cell 可能也会自动设置 backgroundView
关于iphone - 避免 UITableViewCell 的背景 View 忽略单元格边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8218265/