带dynamictypes标签的AutoLayoutdynamicUITableView单元格高度

我正在使用AutoLayout实现我的tableview单元格来显示一个图像和2个标签,使用dynamictypes自适应字体大小。

我实现了估计HeightForRowAtIndexPath,这是非常有道理的,易于使用。

我不使用界面生成器的单元格。 相反,我使用砌体,这应该没有任何区别。

我目前正在努力计算实际的细胞高度。 在更新自动布局代码时,手动计算是一件痛苦而难以维护的事情。

我发现这个StackOverFlow答案: 在UITableView中使用自动布局dynamic单元格布局和variables行高这种解决scheme还应该照顾不同的字体大小,但不适合我。

当我有这样的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>" ) 

我使用以下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); }]; 

最后一行应该表明单元格的高度会自行扩展。

看看AutoLayout冲突的输出,它看起来像它想要自动设置高度为44.0,这是默认值。

编辑 :设置contentView的translatesAutoresizingMaskIntoConstraints为NO

 self.contentView.translatesAutoresizingMaskIntoConstraints = NO; 

创build单元格时修复了碰撞,但是导致行高为零。

我结束了使用下面的代码:

 - (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; } 

在控制器中。

在单元格视图中确保使用以下行

 self.contentView.translatesAutoresizingMaskIntoConstraints = NO;