用于 UITableViewCell 的背景视图的初始框架

发布于 2024-12-03 05:12:32 字数 411 浏览 1 评论 0原文

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

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

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

I'm currently initializing my UITableViewCell's backgroundView's frame with self.frame. This seems to work fine for device orientation changes (cell background fills entire cell, looks fine etc). What's a better frame to use (if any)?

Edit #1: I've also initialized the backgroundView with CGRectZero as the frame. This seems to make no difference (UITableViewCells and backgroundViews function just fine in all interface orientations).

I've also tested setting the autoresizingMask property of the backgroundView. This made no difference either. I'd just like to understand what (if anything) is affected by the backgroundViews initial frame.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬花如无物 2024-12-10 05:12:32

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

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

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

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

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

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

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

Assuming that you are trying to add a UIImageView as backgroundView and that you are trying to resize that imageView, here's my experience:

It seems to be impossible to change the frame of UITableViewCell.backgroundView (or at least not a thing Apple recommends, hence it's not mentioned in the documentation). To use a custom sized UIImageView, eg with a resizable UIImage, as background in a UITableViewCell, I do the following:

1) Create a UIImageView and set its image property to an image of your wish.

2) Add the UIImageView as a subview of the UITableViewCell using the addSubview: message.

3) Send the UIImageView to the back using sendSubviewToBack: message.

This puts your UIImageView behind any other added subviews and you are now able to manipulate the frame of your 'backgroundView' (aka the imageview).

To make sure the imageview will fit the tableViewCell's frame, use cell.frame's height and width properties when calculating the height of your imageview.

眼泪都笑了 2024-12-10 05:12:32

如果您开发自定义表格视图单元格,解决方案是在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

If you develop custom table view cell the solution is to adjust the frame in layoutSubviews method. Here is custom UITableViewCell from one of my projects, I needed 10 points margin from left:

#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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文