iOS Massonry约束自定义TableViewCell自适应行高的约束冲突的问题

来源:互联网 发布:mac导入的照片请解锁 编辑:程序博客网 时间:2024/05/10 17:56
2017-12-14 17:56:56.659 project[4231:2032478] Unable to simultaneously satisfy constraints.    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) [    <MASLayoutConstraint:0x7fef047f19e0 UILabel:0x7fef06824c80.top == UITableViewCellContentView:0x7fef045c40a0.top + 10>,    <MASLayoutConstraint:0x7fef047b95c0 UILabel:0x7fef047f5fe0.top == UILabel:0x7fef06824c80.bottom + 10>,    <MASLayoutConstraint:0x7fef047fa290 UILabel:0x7fef047fbea0.top == UILabel:0x7fef047f5fe0.bottom + 5>,    <MASLayoutConstraint:0x7fef047b8450 UILabel:0x7fef047f55a0.top == UILabel:0x7fef047fbea0.bottom + 5>,    <MASLayoutConstraint:0x7fef047f64b0 UILabel:0x7fef047f9500.top == UILabel:0x7fef047f55a0.bottom + 5>,    <MASLayoutConstraint:0x7fef047f59c0 UILabel:0x7fef047f9500.bottom == UITableViewCellContentView:0x7fef045c40a0.bottom - 10>,    <NSLayoutConstraint:0x7fef06a03f70 UITableViewCellContentView:0x7fef045c40a0.height == 44>]Will attempt to recover by breaking constraint <MASLayoutConstraint:0x7fef047b95c0 UILabel:0x7fef047f5fe0.top == UILabel:0x7fef06824c80.bottom + 10>

这里写图片描述

如上图所示:提示

<NSLayoutConstraint:0x7fef06a03f70 UITableViewCellContentView:0x7fef045c40a0.height == 44>

这表明cell.contentView的约束没有处理,走的是默认的autoresizingMask约束模式,
查看自己写的约束,发现cell的contentView没有用massonry处理(用massonry处理约束的控件,子控件.translatesAutoresizingMaskIntoConstraints = NO;子控件的这个属性自动被设置为NO);

所以只需要在

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {        self.contentView.translatesAutoresizingMaskIntoConstraints = NO;    }return self;}

就可以解决约束冲突的问题
前提控制器写下如下代码,会自动计算行高

- (void)viewDidLoad {    [super viewDidLoad];    self.tableView.rowHeight = UITableViewAutomaticDimension;    self.tableView.estimatedRowHeight = 300;}