title: How to display image in table view cell with dynamic height?
body: <p>A <code>UITableViewCell</code> contains a <code>UIImageView</code> to display images of different width / height ratios.</p>
  
  <p>The table view cell should adapt its height according to the image size ratio. So the image view width should be equal to cell width and the image height is the <code>cell width * (image height / image width)</code> to display the image as <code>aspectFit</code>.</p>
  
  <p>The image view is added with auto-layout constraints:</p>
  
  <pre><code>heightConstraint = imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor, multiplier: 1)
  
  NSLayoutConstraint.activate([
      imageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
      imageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
      imageView.topAnchor.constraint(equalTo: contentView.topAnchor),
      imageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
      heightConstraint
  ])
  </code></pre>
  
  <p>The height constraint is updated when the image is set for the cell:</p>
  
  <pre><code>func updateCell(withImage image: UIImage) {
  
      // Update image
      imageView.image = image
  
      // Deactivate old height constraint
      NSLayoutConstraint.deactivate([heightConstraint])
  
      // Activate new height constraint
      heightConstraint = imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor, multiplier: image.size.height / image.size.width)
      NSLayoutConstraint.activate([heightConstraint])
      }
  }
  </code></pre>
  
  <p>The cell height looks correct on the device, but it logs auto-layout errors:</p>
  
  <pre><code>[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. 
  (
      "&lt;NSLayoutConstraint:0x60700016d600 H:|-(15)-[MyApp.MyImageView:0x61600042b180]   (active, names: '|':UITableViewCellContentView:0x615000056e80 )&gt;",
      "&lt;NSLayoutConstraint:0x60700016d590 MyApp.MyImageView:0x61600042b180.trailing == UITableViewCellContentView:0x615000056e80.trailing - 15   (active)&gt;",
      "&lt;NSLayoutConstraint:0x60700016d4b0 V:|-(15)-[MyApp.MyImageView:0x61600042b180]   (active, names: '|':UITableViewCellContentView:0x615000056e80 )&gt;",
      "&lt;NSLayoutConstraint:0x607000071210 MyApp.MyImageView:0x61600042b180.bottom == UITableViewCellContentView:0x615000056e80.bottom - 15   (active)&gt;",
      "&lt;NSLayoutConstraint:0x607000303bb0 MyApp.MyImageView:0x61600042b180.height == 1.499*MyApp.MyImageView:0x61600042b180.width   (active)&gt;",
      "&lt;NSLayoutConstraint:0x607000073430 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x615000056e80.height == 547   (active)&gt;",
      "&lt;NSLayoutConstraint:0x6070000734a0 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x615000056e80.width == 375   (active)&gt;"
  )
  
  Will attempt to recover by breaking constraint 
  &lt;NSLayoutConstraint:0x607000303bb0 MyApp.MyImageView:0x61600042b180.height == 1.499*MyApp.MyImageView:0x61600042b180.width   (active)&gt;
  
  Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
  The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in &lt;UIKit/UIView.h&gt; may also be helpful.
  </code></pre>
  
  <p>It seems that the added height constraint is conflicting with the <code>UITableViewCellContentView</code> height. I wouldn't have this problem with a <code>UILabel</code> for example because it has an intrinsic content size, but <code>UIImageView</code>'s intrinsic content size is its image size. So what is the solution here without workarounds like calculating each cell height individually?</p>