1

I have a custom tableViewCell and I am trying to set up constraints so that I can use tableView.rowHeight = UITableViewAutomaticDimension.

For simplicity sake let’s say I have a uiTableViewCell whose content view contains only UIImageView. The UIImageView is constrained to maintain a certain aspect ratio and is pinned to the left, right, and top of the cell content view. I want my content view’s height to be driven by the height of the UIImage View.

I’ve been trying to solve this by pinning the content view bottom to the bottom of the UIIMage view, but I’m getting 'unable to satisfy all constraints' errors at runtime. What’s the right way to set this up?

nwales
  • 3,521
  • 2
  • 25
  • 47
  • What iOS version you target? iOS 8+ or prior versions too? – Andrei Konstantinov Mar 08 '15 at 08:31
  • ios 8+ yes. Any os that support UITableViewAutomaticDimension really. – nwales Mar 09 '15 at 13:40
  • Please, confirm, you tried UITableViewAutomaticDimension in iOS 8 and it didn't work? It will not work in iOS 7 but it should in iOS8+. – Andrei Konstantinov Mar 10 '15 at 18:50
  • I am only targeting ios8+ and have not even tested in ios7. The results I am seeing are in ios 8 - where I am getting the 'unable to satisfy all constraints' error. Visually it does appear to be working, but I am concerned by the error and am hoping to find the correct way to set this up. – nwales Mar 11 '15 at 17:13
  • Ah, so the height of the cells itself calculates properly, but log is flooded by constraint error messages. Please, look at those messages - log tells you what constraint causing a problem. You need to make screenshot of your cell with constraint. Let's to figure it out together. – Andrei Konstantinov Mar 17 '15 at 19:19

1 Answers1

0

I know this is kind of late, but i just saw this and hope it helps someone having similar troubles.

Its because you are applying constraints to left and right edge, and is setting the aspect ratio constraints. When this is done, the image view's height is set according to the width, because the image view is pinned to both sides, so the remaining width cannot change, and at the same time, the aspect ratio constraints will come into play.

To solve this issue, what can be done is, remove the constraint to the left or right(keep only one), pin the image view to top and bottom, and set the aspect ratio constraints as well. This will make the width adjust to height and the content view's height gets updated correctly.

If at this point, you get an warning "ambiguous x position for image view", set a width constraint and set its priority to a value less than 1000(which is the priority of all other constraints, I assume).

Skywalker
  • 1,590
  • 1
  • 18
  • 36