A UITableViewCell
contains an UIImageView
and has the height UITableViewAutomaticDimension
.
I want to resize the image view according to the size of its image. The image view should then resize the height of the table view cell.
The vertical layout constraints of the image view in Interface Builder are:
- imageView.topAnchor = cell.contentView.topAnchor
- imageView.bottomAnchor <= cell.contentView.bottomAnchor + 10
- imageView.heightAnchor = 100
I've set a layout constraint as IBOutlet for the height anchor of the image view. As soon as I change its constant the layout breaks and the constraint is ignored. The height of the table view cell does not adapt its size and seems to break the constraint.
The constraint constant is changed at:
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()
}
}
How is it done properly?
Update: The error log break the "width = height" constraint because the layout is actually a bit more complicated then described above: The widthAnchors' constant is changed and the widthAnchor = heightAnchor of the image view.
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.