在项目中会看到打印台上有这样的警告:
2023-05-25 10:04:41.811357+0800 JDDDemo[23487:9760518] [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:0x2834265e0 UILabel:0x100d33a40.left == UIView:0x100d192a0.left + 4>",
"<MASLayoutConstraint:0x283426640 UILabel:0x100d33a40.right == UIView:0x100d192a0.right - 4>",
"<NSLayoutConstraint:0x283316120 UIView:0x100d192a0.width == 1>"
)
Will attempt to recover by breaking constraint
<MASLayoutConstraint:0x283426640 UILabel:0x100d33a40.right == UIView:0x100d192a0.right - 4>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
我的约束是这么写的:
看起来没啥问题,但是貌似masonary给contentView添加了一个默认的约束width=1,然后我自己想要的约束是这个label要距contentView左右边距是4,那就是说这个contentView的宽度至少不小于8,这样就和系统默认的约束width=1相冲突了。
然后看网上的解决办法是设置一个优先级:
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(@0);
make.left.equalTo(@4);
make.right.equalTo(@(-4)).priorityHigh();
make.bottom.equalTo(@0).priorityHigh();
make.height.equalTo(@(height_tags));
}];
或者
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(@0);
make.left.equalTo(@4);
make.right.equalTo(@(-4)).priority(999);
make.bottom.equalTo(@0).priority(999);
make.height.equalTo(@(height_tags));
}];
这样就可以消除掉打印台上的警告了