ios SnapKit 约束Unable to simultaneously satisfy constraints 和UIView-Encapsulated-Layout-Height的警告
程序员文章站
2022-04-25 20:09:03
...
最近在自定义UICollectionViewCell,cell里面UITextView约束如下:
inputTextView.snp.makeConstraints { (make) in
make.left.top.equalTo(self).offset(kAdaptedWidth(10))
make.right.bottom.equalTo(self).offset(kAdaptedWidth(-10))
}
结果编译报以下打印警告:
[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.
<__NSArrayI 0x60000128e9d0>(
<SnapKit.LayoutConstraint:[email protected]#22 UITextView:0x7fbb870f1000.top ==app.GoodsTypeInputCollectionViewCell:0x7fbb86c4a430.top + 10.0>,
<SnapKit.LayoutConstraint:[email protected]#24 UITextView:0x7fbb870f1000.bottom == app.HYGoodsTypeInputCollectionViewCell:0x7fbb86c4a430.bottom - 10.0>,
<NSLayoutConstraint:0x600003f528a0 'UIView-Encapsulated-Layout-Height' app.GoodsTypeInputCollectionViewCell:0x7fbb86c4a430.height == 0 (active)>
)
Will attempt to recover by breaking constraint
<SnapKit.LayoutConstraint:[email protected]#24 UITextView:0x7fbb870f1000.bottom == app.GoodsTypeInputCollectionViewCell:0x7fbb86c4a430.bottom - 10.0>
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.
原因是我的cell刚开始高度为0,在某些条件下,会变成另外一个height,然而由于CollectionView会默认添加两个约束就是UIView-Encapsulated-Layout-Width 和UIView-Encapsulated-Layout-Hight保证大小适中,但是我的cell的高度刚开始默认为0,它的系统默认约束和我的设置的约束权限冲突,造成的。
因此只需要降低自己约束权限就行了:
inputTextView.snp.makeConstraints { (make) in
make.left.top.equalTo(self).offset(kAdaptedWidth(10))
make.right.equalTo(self).offset(kAdaptedWidth(-10))
make.bottom.equalTo(self).offset(kAdaptedWidth(-10)).priorityLow()
}
这样都没有约束警告log
上一篇: linux中日志管理及日志轮询