Objective c UITableViewCell使用的初始帧';中国背景
我当前正在使用Objective c UITableViewCell使用的初始帧';中国背景,objective-c,ios,uitableview,frame,Objective C,Ios,Uitableview,Frame,我当前正在使用self.frame初始化UITableViewCell的backgroundView框架。这似乎可以很好地适应设备方向的变化(单元格背景填充整个单元格,外观良好等)使用哪种框架更好(如果有)? Edit#1:我还用CGRectZero作为框架初始化了背景视图。这似乎没有什么区别(UITableViewCells和backgroundViews在所有界面方向上都可以正常工作) 我还测试了backgroundView的autoresizingMask属性的设置。这也没什么区别我只想了
self.frame
初始化UITableViewCell的backgroundView框架。这似乎可以很好地适应设备方向的变化(单元格背景填充整个单元格,外观良好等)使用哪种框架更好(如果有)?
Edit#1:我还用CGRectZero
作为框架初始化了背景视图。这似乎没有什么区别(UITableViewCells和backgroundViews在所有界面方向上都可以正常工作)
我还测试了backgroundView的
autoresizingMask
属性的设置。这也没什么区别我只想了解背景视图初始帧会影响哪些内容(如果有)。假设您正在尝试添加UIImageView作为背景视图,并且正在尝试调整该imageView的大小,以下是我的经验:
似乎无法更改UITableViewCell.backgroundView的框架(或者至少不是苹果公司推荐的,因此文档中没有提及)。要使用自定义大小的UIImageView(例如具有可调整大小的UIImage)作为UITableViewCell中的背景,我执行以下操作:
1) 创建UIImageView并将其图像属性设置为所需的图像
2) 使用addSubview:消息将UIImageView添加为UITableViewCell的子视图
3) 使用sendSubviewToBack:message将UIImageView发送到后面
这将UIImageView置于任何其他添加的子视图之后,您现在可以操纵“背景视图”(也称为imageview)的框架
要确保imageview适合tableViewCell的边框,请在计算imageview的高度时使用cell.frame的高度和宽度属性。如果开发自定义表格视图单元格,解决方案是在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
背景的框架将等于单元格的边界,不一定是单元格的框架。这很有趣。苹果公司有没有关于这个工作原理的文档?我找了不少,但什么也没找到。