forkingdog / UITableView-FDTemplateLayoutCell

Template auto layout cell for automatically UITableViewCell height calculating
MIT License
9.94k stars 2.01k forks source link

fixed bug on iOS 10.2 #264

Closed wanhmr closed 7 years ago

wanhmr commented 7 years ago

fixed carsh on iOS 10.2 add use the UILabel calculate height set preferredMaxLayoutWidth property

wanhmr commented 7 years ago

cell.contentView 调用 intrinsicContentSize 方法会增加 4 个约束

<NSLayoutConstraint:0x174285230 UITableViewCellContentView:0x11bed4d70.height == 154.5> <NSAutoresizingMaskLayoutConstraint:0x1742850f0 UITableViewCellContentView:0x11bed4d70.(null) == 0> <NSAutoresizingMaskLayoutConstraint:0x174285140 UITableViewCellContentView:0x11bed4d70.(null) == 0> <NSLayoutConstraint:0x174284d30 UITableViewCellContentView:0x11bed4d70.width == 362>

cell.contentView 调用 systemLayoutSizeFittingSize 会隐式地调用 intrinsicContentSize

NSLayoutConstraint *widthFenceConstraint = [NSLayoutConstraint constraintWithItem:cell.contentView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:contentViewWidth]; [cell.contentView addConstraint:widthFenceConstraint];

添加宽度约束后 fittingHeight = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

cell.contentView 调用 systemLayoutSizeFittingSize 会隐式地调用 intrinsicContentSize

<NSLayoutConstraint:0x17028d480 UITableViewCellContentView:0x11bed4d70.width == 375> <NSLayoutConstraint:0x174285230 UITableViewCellContentView:0x11bed4d70.height == 154.5> <NSAutoresizingMaskLayoutConstraint:0x1742850f0 UITableViewCellContentView:0x11bed4d70.(null) == 0> <NSAutoresizingMaskLayoutConstraint:0x174285140 UITableViewCellContentView:0x11bed4d70.(null) == 0> <NSLayoutConstraint:0x174284d30 UITableViewCellContentView:0x11bed4d70.width == 362>

计算出高度,然后删除约束 [cell.contentView removeConstraint:widthFenceConstraint];

<NSLayoutConstraint:0x17028d480 UITableViewCellContentView:0x11bed4d70.width == 375> <NSLayoutConstraint:0x174285230 UITableViewCellContentView:0x11bed4d70.height == 154.5> <NSAutoresizingMaskLayoutConstraint:0x1742850f0 UITableViewCellContentView:0x11bed4d70.(null) == 0> <NSAutoresizingMaskLayoutConstraint:0x174285140 UITableViewCellContentView:0x11bed4d70.(null) == 0> <NSLayoutConstraint:0x174284d30 UITableViewCellContentView:0x11bed4d70.width == 362>

可以看出,删除约束后,隐式增加的约束 constant 从 362 变成了 375,奇怪。