【发布时间】:2016-06-10 04:38:52
【问题描述】:
我正在尝试创建一个表格视图单元格,左侧是图像,右侧是内容框。图片应该是正方形的,并且是单元格宽度的 20%,内容框(下面代码中的background)至少应该和图片一样大。最后,我想尊重 tableview 本身的布局边距。
除了最后一步,这很好,尊重表格视图的边距。设置这个会导致我的内容框在 tableview 很宽时设置过大或过小。
// Base class
[self.background setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
[self.background mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.equalTo(self.contentView).with.offset(8.0).with.priorityMedium();
make.trailing.equalTo(self.contentView).with.offset(-8.0).with.priorityMedium();
make.top.equalTo(self.contentView).with.offset(4.0);
make.bottom.equalTo(self.contentView).with.offset(-4.0);
}];
// **** This block results in respecting tableview margins, but height constraints of background are not respected. Comment it out to create image "B".
[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.equalTo(self.mas_leadingMargin);
make.trailing.equalTo(self.mas_trailingMargin);
make.top.equalTo(self.mas_top).with.priorityHigh();
make.bottom.equalTo(self.mas_bottom).with.priorityHigh();
}];
// **** End problematic block
// Subclass
[self.courseImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.equalTo(self.contentView).with.offset(8.0);
make.height.and.width.equalTo(self.contentView.mas_width).multipliedBy(.20).with.priorityHigh();
make.centerY.equalTo(contentView);
}];
[self.background mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.equalTo(self.courseImageView.mas_trailing).with.offset(8.0);
make.height.greaterThanOrEqualTo(self.courseImageView).with.priorityMedium();
make.height.equalTo(self.courseImageView).with.priorityLow();
}];
B.)在这里,边距得到尊重,但 background 已扩大到荒谬的数量。此外,当它像这样重新加载时,表格视图会向上滚动。
【问题讨论】:
标签: ios uitableview autolayout masonry