纯代码使用Masonry进行子控件约束的时候(尤其是tableViewCell的子控件自动适应行高),经常会出现下面约束警告
2017-12-29 16:24:42.645364+0800 project[3804:770025] [LayoutConstraints] 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.
[
<MASLayoutConstraint:0x6040002b7d60 UILabel:0x7fa5e0c5c490.top == UITableViewCellContentView:0x7fa5e0c5c280.top + 10>,
<MASLayoutConstraint:0x6040002b85a0 UILabel:0x7fa5e0c5c770.top == UILabel:0x7fa5e0c5c490.bottom + 10>,
<MASLayoutConstraint:0x6040002b8f00 UILabel:0x7fa5e0c5d010.top == UILabel:0x7fa5e0c5c770.bottom + 5>,
<MASLayoutConstraint:0x6040002b94a0 UILabel:0x7fa5e0c5d5d0.top == UILabel:0x7fa5e0c5d010.bottom + 5>,
<MASLayoutConstraint:0x6040002b9800 UILabel:0x7fa5e0c5d8b0.top == UILabel:0x7fa5e0c5d5d0.bottom + 5>,
<MASLayoutConstraint:0x6040002b9aa0 UILabel:0x7fa5e0c5d8b0.bottom == UITableViewCellContentView:0x7fa5e0c5c280.bottom - 10>,
<NSLayoutConstraint:0x600000290810 UITableViewCellContentView:0x7fa5e0c5c280.height == 43.6667>
]
Will attempt to recover by breaking constraint
<MASLayoutConstraint:0x6040002b85a0 UILabel:0x7fa5e0c5c770.top == UILabel:0x7fa5e0c5c490.bottom + 10>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
如上图所示:提示
Will attempt to recover by breaking constraint
<MASLayoutConstraint:0x7fef047b95c0 UILabel:0x7fef047f5fe0.top == UILabel:0x7fef06824c80.bottom + 10>
- 1
- 2
网上查的解决方案都是下面这种…………
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
就可以解决约束冲突的问题
然而尝试了下,并没有什么卵用!!!!! 继续报下面的警告
2017-12-29 16:34:15.155566+0800 project[3867:812969] [LayoutConstraints] Changing the translatesAutoresizingMaskIntoConstraints property of the contentView of a UITableViewCell is not supported and will result in undefined behavior, as this property is managed by the owning UITableViewCell. Cell: <ZYVendorManageCell: 0x7fcac0864800; baseClass = UITableViewCell; frame = (0 0; 320 44); layer = <CALayer: 0x60400042c680>>
2017-12-29 16:34:15.168135+0800 project[3867:812969] [LayoutConstraints] 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.
[
<MASLayoutConstraint:0x6040002b35c0 UILabel:0x7fcabedbb9b0.left == UITableViewCellContentView:0x7fcabef07860.left + 10>,
<MASLayoutConstraint:0x6040002b3680 UILabel:0x7fcabedbb9b0.right == UITableViewCellContentView:0x7fcabef07860.right - 10>,
<NSLayoutConstraint:0x6000002828a0 UITableViewCellContentView:0x7fcabef07860.width == 0>
]
Will attempt to recover by breaking constraint
<MASLayoutConstraint:0x6040002b3680 UILabel:0x7fcabedbb9b0.right == UITableViewCellContentView:0x7fcabef07860.right - 10>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
前提控制器写下如下代码,会自动计算行高
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 300;
}
- 1
- 2
- 3
- 4
- 5
解决方案
//这句代码并没有起什么作用
self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
- 1
- 2
//真正的问题出现在这里 -- 最终发现其实是这个约束出现了问题,需要调整约束级别
Will attempt to recover by breaking constraint
<MASLayoutConstraint:0x7fef047b95c0 UILabel:0x7fef047f5fe0.top == UILabel:0x7fef06824c80.bottom + 10>
- 1
- 2
- 3
约束警告问题,完美解决!!!!