[英]AutoLayout in DispatchQueue.main does not work
我對自動版式有疑問。
背景=>我想在UITableViewCell中排列UIImageView,使UIImageView異步獲取圖像,並通過AutoLayout使用圖像的縱橫比動態更改單元格的高度。 其中,UIImageView在我的自定義UIView之上,而后者又在Cell的ContentView上。
AutoLayout與此代碼配合良好,沒有任何錯誤。
private var contentHeightConstraint = NSLayoutConstraint()
// this method called in constructor
func setupAutolayout() {
NSLayoutConstraint.activate([
messageView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8),
messageView.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: 16),
messageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8),
messageView.widthAnchor.constraint(equalToConstant: 240),
contentImageView.topAnchor.constraint(equalTo: messageView.topAnchor),
contentImageView.leftAnchor.constraint(equalTo: messageView.leftAnchor),
contentImageView.rightAnchor.constraint(equalTo: messageView.rightAnchor),
contentImageView.bottomAnchor.constraint(equalTo: messageView.bottomAnchor)
])
}
// After view loaded, this method called
func something() {
contentHeightConstraint = contentImageView.heightAnchor.constraint(equalToConstant: 150)
contentHeightConstraint.isActive = true
}
但是將DispatchQueue.main中的代碼用作異步任務(將在Main線程中執行),AutoLayout效果不佳。
這會導致[LayoutConstraints] Unable to simultaneously satisfy constraints
錯誤。
private var contentHeightConstraint = NSLayoutConstraint()
// this method called in constructor
func setupAutolayout() {
NSLayoutConstraint.activate([
messageView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8),
messageView.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: 16),
messageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8),
messageView.widthAnchor.constraint(equalToConstant: 240),
contentImageView.topAnchor.constraint(equalTo: messageView.topAnchor),
contentImageView.leftAnchor.constraint(equalTo: messageView.leftAnchor),
contentImageView.rightAnchor.constraint(equalTo: messageView.rightAnchor),
contentImageView.bottomAnchor.constraint(equalTo: messageView.bottomAnchor)
])
}
// After view loaded, this method called
func something() {
DispatchQueue.main.async {
// of course, exec in MainThread
self.contentHeightConstraint = contentImageView.heightAnchor.constraint(equalToConstant: 150)
self.contentHeightConstraint.isActive = true
}
}
這就是錯誤消息(此錯誤消息僅在異步運行時出現)。
2019-03-31 13:10:06.791758+0900 app[42515:4243718] [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:0x600002a3dbd0 V:|-(8)-[UIView:0x7fe775f3f830] (active, names: '|':UITableViewCellContentView:0x7fe775f3c210 )>",
"<NSLayoutConstraint:0x600002a3f1b0 UIView:0x7fe775f3f830.bottom == UITableViewCellContentView:0x7fe775f3c210.bottom - 8 (active)>",
"<NSLayoutConstraint:0x600002a32a80 V:|-(0)-[UIImageView:0x7fe775f40690] (active, names: '|':UIView:0x7fe775f3f830 )>",
"<NSLayoutConstraint:0x600002a32350 UIImageView:0x7fe775f40690.bottom == UIView:0x7fe775f3f830.bottom (active)>",
"<NSLayoutConstraint:0x600002a2c190 UIImageView:0x7fe775f40690.height == 150 (active)>",
"<NSLayoutConstraint:0x600002a2d310 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fe775f3c210.height == 116 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600002a2c190 UIImageView:0x7fe775f40690.height == 150 (active)>
點
需要注意的一點是,如果您在UIImageView的HeihgtAnchor之后正常添加約束,它會按預期工作,但是異步添加約束將無法正常工作。
附加信息
* 桌面設置
table.estimatedRowHeight = 800
table.rowHeight = UITableView.automaticDimension
您是否同時使用底部約束和高度? 如果是這樣,那將行不通。
您可以嘗試通過移除UIImageView
高度約束或UIImageView
到UIView
底部約束來進行嘗試。
注意:請勿在后台線程中使用與視圖相關的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.