我对自动版式有疑问。
background =>我想在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
答案 0 :(得分:1)
您是否同时使用底部约束和高度?如果是这样,那将行不通。
您可以尝试删除UIImageView
的高度限制或将UIImageView
的底部限制去除为UIView
。
注意: 不要在后台线程中使用与视图相关的代码。