【问题标题】:Initial frame to use for UITableViewCell's backgroundView用于 UITableViewCell 的 backgroundView 的初始帧
【发布时间】:2011-11-12 10:30:03
【问题描述】:

我目前正在使用self.frame 初始化我的 UITableViewCell 的 backgroundView 框架。这似乎适用于设备方向更改(单元格背景填充整个单元格,看起来很好等)。 有什么更好的框架(如果有的话)?

编辑#1:我还用CGRectZero 作为框架初始化了backgroundView。这似乎没有什么区别(UITableViewCells 和 backgroundViews 在所有界面方向上都可以正常工作)。

我还测试了设置 backgroundView 的 autoresizingMask 属性。这也没什么区别。 我只是想了解什么(如果有的话)受 backgroundViews 初始帧的影响。

【问题讨论】:

  • 背景的框架将等于单元格的边界,不一定是单元格的框架。
  • 这很有趣。任何有关其工作原理的 Apple 文档?我看了很多,但没有找到任何东西。

标签: objective-c ios uitableview frame


【解决方案1】:

假设您正在尝试将 UIImageView 添加为 backgroundView 并且您正在尝试调整该 imageView 的大小,这是我的经验:

似乎不可能改变 UITableViewCell.backgroundView 的框架(或者至少不是苹果推荐的,因此文档中没有提到)。要使用自定义大小的 UIImageView,例如使用可调整大小的 UIImage,作为 UITableViewCell 中的背景,我执行以下操作:

1) 创建一个 UIImageView 并将其 image 属性设置为您希望的图像。

2) 使用 addSubview: 消息将 UIImageView 添加为 UITableViewCell 的子视图。

3) 使用 sendSubviewToBack: 消息将 UIImageView 发送到后面。

这会将您的 UIImageView 置于任何其他添加的子视图之后,您现在可以操作“背景视图”(也称为图像视图)的框架。

为确保 imageview 适合 tableViewCell 的框架,请在计算 imageview 的高度时使用 cell.frame 的 height 和 width 属性。

【讨论】:

    【解决方案2】:

    如果您开发自定义表格视图单元格,解决方案是在 layoutSubviews 方法中调整框架。这是我的一个项目中的自定义 UITableViewCell,我需要从左边算起 10 个点的边距:

    #import "TETopicCell.h"
    #import "UIColor+Utils.h"
    
    @implementation TETopicCell
    
    @synthesize topicTitleLabel = _topicTitleLabel;
    
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
    
            UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"theme_btn_yellow"]];
            bgImageView.contentMode = UIViewContentModeTopLeft;
            bgImageView.frame = CGRectMake(0.0f, 0.0f, 239.0f, 42.0f);
            self.backgroundView = bgImageView;
    
            _topicTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(46.0f, 0.0f, 206.0f, 42.0f)];
            _topicTitleLabel.backgroundColor = [UIColor clearColor];
            _topicTitleLabel.textColor = [UIColor colorWithR:116 G:74 B:1];
    
            [self.contentView addSubview:_topicTitleLabel];
        }
        return self;
    }
    
    - (void)layoutSubviews
    {
        [super layoutSubviews];
    
        // update frame here
        self.backgroundView.frame = CGRectOffset(self.backgroundView.frame, 10.0f, 0.0f);
    
        // and here      
        if (self.selectedBackgroundView){
            self.selectedBackgroundView.frame = CGRectOffset(self.selectedBackgroundView.frame, 10.0f, 0.0f);
        }
    }
    
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated
    {
        [super setSelected:selected animated:animated];
    
        if (selected) {
            UIImageView *selectedImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"theme_btn_red"]];
            selectedImageView.contentMode = UIViewContentModeTopLeft;
            selectedImageView.frame = CGRectMake(0.0f, 0.0f, 239.0f, 42.0f);
    
            self.selectedBackgroundView = selectedImageView;
            [_topicTitleLabel setTextColor:[UIColor colorWithR:255 G:211 B:211]];
        }
        else {
            self.selectedBackgroundView = nil;
            [_topicTitleLabel setTextColor:[UIColor colorWithR:116 G:74 B:1]];
        }
    }
    
    @end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多