iphone - 避免 UITableViewCell 的背景 View 忽略单元格边界

标签 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 无效。

最佳答案

就像我在评论中发表的那样:我会在 self 上启用剪辑,因为 subview backgroundView 太大了。

另外:我会用

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

也尝试设置框架,因为 Cell 可能也会自动设置 backgroundView

的框架

关于iphone - 避免 UITableViewCell 的背景 View 忽略单元格边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8218265/

相关文章:

ios - 警告 : attempt to present

ios - 我如何判断屏幕何时显示在 iOS 中?

ios - 无法理解 Firebase 数据库规则

iphone - MKStoreKit 购买功能不执行任何操作

ios - 在 UICollectionView 单元格中添加 UIViewController

ios - 设备无法使用 Firebase 身份验证电话号码

iphone - 使用图像快速加载 UIview 背景

iphone - 从 NSString 创建安全文件名?

iphone - 为什么我的 UItableView 不水平滚动,而我将 ContentSize 设置得更大?

iphone - for循环中的多个uiimageview