在启用了自动布局的故事板中定义的UITableViewController
中,以编程方式创建UITableViewCell
子类(无IB原型单元格),并使用PureLayout通过updateConstraints
设置约束。
由于某种原因,表视图使每个contentView具有不同的宽度,而不是使用UITableView
的const宽度。
这里是init
子类的updateConstraints
和UITableViewCell
方法 -
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
_nameLabel = [GTPaddedLabel newAutoLayoutView];
_nameLabel.font = [BQFontUtil standardFontSize:StandardFontSize1a bold:YES italic:NO];
_nameLabel.textColor = [UIColor blackColor];
_drugType = [UILabel newAutoLayoutView];
_drugType.font = [BQFontUtil standardFontSize:StandardFontSize0 bold:NO italic:NO];
_drugType.textColor = [UIColor darkGrayColor];
_drugType.textAlignment = NSTextAlignmentRight;
_genericsLabel = [UILabel newAutoLayoutView];
_genericsLabel.font = [BQFontUtil standardFontSize:StandardFontSize0 bold:NO italic:NO];
_genericsLabel.textColor = [UIColor darkGrayColor];
_regimen = [UILabel newAutoLayoutView];
_regimen.font = [BQFontUtil standardFontSize:StandardFontSize0 bold:NO italic:NO];
_regimen.textColor = [UIColor darkGrayColor];
[self.contentView addSubview:_nameLabel];
[self.contentView addSubview:_drugType];
[self.contentView addSubview:_genericsLabel];
[self.contentView addSubview:_regimen];
self.contentView.backgroundColor = [UIColor redColor];
}
return self;
}
- (void)updateConstraints {
if (!constraintsCreated) {
// [self.contentView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[UIView autoSetPriority:UILayoutPriorityRequired forConstraints:^{
[_nameLabel autoSetContentCompressionResistancePriorityForAxis:ALAxisVertical];
}];
[_nameLabel autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(8, 8, 0, 8) excludingEdge:ALEdgeBottom];
[_genericsLabel autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:16];
[_genericsLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:_nameLabel withOffset:4];
[_genericsLabel autoSetDimension:ALDimensionHeight toSize:21];
[_drugType autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:16];
[_drugType autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:_nameLabel withOffset:4];
[_drugType autoPinEdge:ALEdgeLeading toEdge:ALEdgeTrailing ofView:_genericsLabel withOffset:8];
[_drugType autoSetDimension:ALDimensionHeight toSize:21];
[_regimen autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:_genericsLabel withOffset:2];
[_regimen autoSetDimension:ALDimensionHeight toSize:21];
[_regimen autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:16];
[_regimen autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:16];
[_regimen autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:8];
constraintsCreated = YES;
}
[super updateConstraints];
}
为什么contentView(棕色)的宽度与表格视图的宽度不同?
答案 0 :(得分:0)
也许这是一种解决方法,当我将contentView的宽度限制为单元格的宽度时,它运行良好 -
- (void)updateConstraints {
if (!constraintsCreated) {
[self.contentView autoSetDimension:ALDimensionWidth toSize:self.frame.size.width];
...
}
}