在UITableViewCell中嵌入UITableView的時候,引發(fā)以下沖突問題:
2019-11-11 19:20:44.785146+0800 dudu_oc_master[11212:370558] [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.
(
"<NSLayoutConstraint:0x6000000f82d0 UITableView:0x7fb174989000.height == 204.203>",
"<NSLayoutConstraint:0x60000000dc70 UIImageView:0x7fb176459e00.top == UITableViewCellContentView:0x7fb176459c60.top + 8>",
"<NSLayoutConstraint:0x60000000dd10 UILabel:0x7fb176459fd0.top == UIImageView:0x7fb176459e00.top>",
"<NSLayoutConstraint:0x60000000ddb0 UILabel:0x7fb17645a4f0.top == UIButton:0x7fb17645a240.bottom + 4.5>",
"<NSLayoutConstraint:0x60000000de50 UIButton:0x7fb173e44090.top == UILabel:0x7fb17645a4f0.bottom + 8>",
"<NSLayoutConstraint:0x60000000def0 UITableViewCellContentView:0x7fb176459c60.bottom >= UITableView:0x7fb174989000.bottom + 12>",
"<NSLayoutConstraint:0x60000000df40 UITableView:0x7fb174989000.top == UIButton:0x7fb173e44090.bottom + 8>",
"<NSLayoutConstraint:0x60000000e0d0 UIButton:0x7fb17645a240.centerY == UILabel:0x7fb176459fd0.centerY>",
"<NSLayoutConstraint:0x6000000f86e0 UITableViewCellContentView:0x7fb176459c60.height == 85>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x6000000f82d0 UITableView:0x7fb174989000.height == 204.203>
這個問題發(fā)生在我進行折疊顯示內層tableView的時候(通過更新約束),比較怪異的是居然與UITableViewCellContentView有關,我在項目中沒有手動給UITableViewCellContentView設置過高度,估計是系統(tǒng)auto layout過程中生成的。沖突的原因很明顯,由于cell復用的原因,復用時UITableViewCellContentView的高度是85,然后將內層的tableView高度約束調整到了204.20,后者明顯大于前者,而后者又包含于前者,就引發(fā)了沖突。
最后發(fā)現(xiàn)解決問題如下:
將內層tableview到contentView的bottom的約束的priority設置得比UITableViewCellContentView.height更要低就可以了。
這樣復用時就會優(yōu)先應用UITableViewCellContentView.height去做布局,但是請記住你更新了內層的tableview高度此時并沒有得到體現(xiàn),需要執(zhí)行以下代碼:
外層tableView.beginUpdates()
外層tableView.endUpdates()