UITableViewCell
包含UIImageView
,其高度为UITableViewAutomaticDimension
。
我想根据图像大小调整图像视图的大小。然后,图像视图应调整表格视图单元格的高度。
Interface Builder中图像视图的垂直布局约束是:
我已将布局约束设置为图像视图的高度锚点的IBOutlet。一旦我改变其常量,布局就会中断并忽略约束。表格视图单元格的高度不适应其大小,似乎打破了约束。
约束常量在:
处更改func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as! MyCell
cell.image = image
cell.updateConstraint()
return cell
}
class MyCell {
func updateConstraint() {
imageViewHeightConstraint.constant = image.size.height // This line already throws the UIViewAlertForUnsatisfiableConstraints exception
imageImageView.setNeedsLayout()
}
}
如何正确完成?
更新 错误日志打破了" width = height"约束因为布局实际上比上面描述的要复杂一点:widthAnchors'常量被更改,图像视图的widthAnchor = heightAnchor。
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:0x7f98be1a22b0 H:[UIImageView:0x7f98be7d36e0(96)]>",
"<NSLayoutConstraint:0x7f98be158470 UIImageView:0x7f98be7d36e0.width == UIImageView:0x7f98be7d36e0.height>",
"<NSLayoutConstraint:0x7f98be1273e0 V:|-(5)-[UIImageView:0x7f98be7d36e0] (Names: '|':UIView:0x7f98be12d860 )>",
"<NSLayoutConstraint:0x7f98be133040 V:[UIImageView:0x7f98be7d36e0]-(5)-| (Names: '|':UIView:0x7f98be12d860 )>",
"<NSLayoutConstraint:0x7f98be1929e0 V:|-(5)-[UIView:0x7f98be12d860] (Names: '|':UITableViewCellContentView:0x7f98be1582f0 )>",
"<NSLayoutConstraint:0x7f98be12dd10 V:[UIView:0x7f98be12d860]-(>=10)-| (Names: '|':UITableViewCellContentView:0x7f98be1582f0 )>",
"<NSLayoutConstraint:0x7f98be7d66a0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7f98be1582f0(89)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7f98be158470 UIImageView:0x7f98be7d36e0.width == UIImageView:0x7f98be7d36e0.height>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.