Kra*_*eFx 2 objective-c uitableview ios autolayout
我正在使用AutoLayout实现我的tableview单元格来显示图像和2个标签,使用动态类型来显示自适应字体大小.
我实现了estimatedHeightForRowAtIndexPath,它非常有意义且易于使用.
我没有为单元格使用界面构建器.相反,我使用砌体,这应该没有任何区别.
我目前正在努力计算实际的细胞高度.在更新自动布局代码时,手动计算是一件痛苦的事情并且难以维护.
我发现这个StackOverFlow答案:在UITableView中使用自动布局来获得动态单元格布局和可变行高度 此解决方案还应该考虑不同的字体大小,但对我来说不起作用.
当我有这样的AutoLayout系统时:UILabel顶部带有填充的contentView,另一个UILabel到带有填充的contentView底部,它应该自动计算单元格高度.
但相反,它会导致以下AutoLayout冲突:
UIView property translatesAutoresizingMaskIntoConstraints)
(
"<MASLayoutConstraint:0xc506be0 UIImageView:0xc5070a0.height == 150>",
"<MASLayoutConstraint:0xc506d90 UIImageView:0xc5070a0.top == UITableViewCellContentView:0xc5076e0.top + 10>",
"<MASLayoutConstraint:0xc506990 UILabel:0xc507450.top == UIImageView:0xc5070a0.bottom + 10>",
"<MASLayoutConstraint:0xc506630 UILabel:0xc507260.top == UILabel:0xc507450.bottom>",
"<MASLayoutConstraint:0xc506530 UILabel:0xc507260.bottom == UITableViewCellContentView:0xc5076e0.bottom>",
"<NSAutoresizingMaskLayoutConstraint:0xc3d05a0 UITableViewCellContentView:0xc5076e0.height == 44>"
)
Run Code Online (Sandbox Code Playgroud)
我使用以下AutoLayout代码:
[self.subtitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.titleLabel.mas_bottom);
make.right.equalTo(self.contentView.mas_right).with.offset(-GCBaconCellRowPadding);
make.left.equalTo(self.contentView.mas_left).with.offset(GCBaconCellRowPadding);
make.bottom.equalTo(self.contentView.mas_bottom);
}];
Run Code Online (Sandbox Code Playgroud)
最后一行应表明单元格的高度自身延伸.
查看AutoLayout冲突的输出,看起来它想要自动将高度设置为44.0,这是默认值.
编辑:将contentView的translatesAutoresizingMaskIntoConstraints设置为NO
self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
Run Code Online (Sandbox Code Playgroud)
创建单元格时会修复碰撞,但会导致行高为零.
我最终使用以下代码:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
static GCBaconCell *offscreenCell;
if (!offscreenCell)
{
offscreenCell = [[GCBaconCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"nothing"];
}
// configure offscreenCell ...
[offscreenCell.contentView setNeedsLayout];
[offscreenCell.contentView layoutIfNeeded];
CGSize maximumSize = CGSizeMake(320.0, UILayoutFittingCompressedSize.height);
CGFloat height = [offscreenCell.contentView systemLayoutSizeFittingSize:maximumSize].height;
return height;
}
Run Code Online (Sandbox Code Playgroud)
在控制器中.
在单元格视图中,请确保使用以下行
self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9415 次 |
| 最近记录: |