103

Despite having set constraints to all elements, including the vertical ones needed for the cell to calculate its height, auto-layout seems to be ignored: all cells are squeezed.

Here's a screenshot of the result and of the constraints in the storyboard:

enter image description here

enter image description here

In the VC that holds the tableView, here's the code in viewDidLoad:

tableView.estimatedRowHeight = 120.0
tableView.rowHeight = UITableViewAutomaticDimension

Commenting out the second line gives cells with a height of 120.0 but Autolayout is ignored as well.


Update

To simplify the interface, I've left a single label with, as constraints:

  • Leading space to superview
  • Top space to superview
  • Fixed width and height (100 & 100)
  • Bottom space to container margin to make sure that the cell has all vertical constraints to determine its height

And with this simplified interface, auto-layout is still not taken into account, which hints me that the problem did not come from badly set constraints.

In the Size Inspector, the row height is set on 120 and Custom is checked. The cell has the right custom class, the cell reuse identifier is correct.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Kqtr
  • 5,824
  • 3
  • 25
  • 32
  • Check your debug terminal - are there any NSLayoutConstraint logs in there suggesting something isn't working as expected? – Matthew Hallatt Apr 10 '17 at 09:49
  • Nothing appears in the debug terminal @MatthewHallatt, thank you. – Kqtr Apr 10 '17 at 09:51
  • @Kqtr try commenting these two lines and then run it normally once to check is there any change? – Tushar Sharma Apr 10 '17 at 09:53
  • @TusharSharma Commenting out the two lines has the same effect as commenting out the rowHeight line only: the cell has a height of 120.0, but auto-layout doesn't work. – Kqtr Apr 10 '17 at 09:55
  • @Kqtr I guess you are not specifying constraints properly. – Tushar Sharma Apr 10 '17 at 10:01
  • 1
    Does interface builder show any constraints as missing or ambiguous? Check for the little yellow or red exclamation mark. – Matthew Hallatt Apr 10 '17 at 10:01
  • @MatthewHallatt No errors or warnings – Kqtr Apr 10 '17 at 10:05
  • And you're not also overriding `heightForCellAtIndexPath` in your delegate/data source? My only other suggestion would be to remove all the constraints from the cell and add them back in one item at a time. It is most likely an issue with the constraints themselves. See at what point the cell stops working. – Matthew Hallatt Apr 10 '17 at 10:07
  • @TusharSharma Might be. Would all constraints be ignored if only one was not set properly? – Kqtr Apr 10 '17 at 10:08
  • @MatthewHallatt Not overriding `heightForCellAtIndexPath`, I do not use a delegate, only a data source. I guess you're right, I'll just delete and replace all contraints and see what happens. Thank you! – Kqtr Apr 10 '17 at 10:12
  • @MatthewHallatt I've tried deleting constraints and simplifying contraints, still doesn't work. Can you have a look at the update in my answer? – Kqtr Apr 10 '17 at 10:31

2 Answers2

281

Auto-layout was ignored because both the prototype cell AND the UIView of the cell had been given the custom cell class in IB.

Setting the UIView back to UIView class solved the problem.

just to be über clear

MoVod
  • 1,083
  • 10
  • 10
Kqtr
  • 5,824
  • 3
  • 25
  • 32
  • 1
    I'm running into this issue even with no custom class for the `Content View` has anyone else encountered this? – Destiny Faith Dec 01 '20 at 14:25
  • @Destiny Faith Did you resolve the issue? – Arjun Mar 30 '21 at 13:03
  • @Arjun in the UITableViewCell's attributes pane, check the Style dropdown. If it's not on Custom, it'll force you to stick with the ui elements that go with the selected built-in style. That's most likely your issue – NickSpag Jul 24 '21 at 20:30
  • You are my hero. – Rydell Dec 09 '21 at 00:48
  • That was too much time spent on what was ultimately such a trivial little fix. Why does it do this? – alstr Dec 15 '21 at 09:44
  • 1
    @alstr you probably just have to be more careful with the selected views. When clicking stuff on a storyboard it's easy to think you have something selected when it is in fact something else. It's harder to see when the views overlap as in the container and the content view. – ZShock Apr 22 '22 at 14:00
  • You saved tons of my time. Really thanks! – 2nebin Dec 07 '22 at 07:32
-3

I think the problem is that you are implementing :

func tableView(_ tableView: UITableView, heightForRowAt indexPath: 
IndexPath) -> CGFloat {

 return x
}

You need to remove this function inorder for the tableview to calculate its automatic height for cells.

Jaafar Barek
  • 420
  • 3
  • 9
  • Thanks, I unfortunately do not override it: I actually do not even have a delegate for this tableView, only a data source. – Kqtr Apr 10 '17 at 10:10
  • Did you set the textview constraints to all the for edges of the cell? And did you disable the textView's scrolling? – Jaafar Barek Apr 10 '17 at 10:13
  • The multi-line label (0 lines) has constraints for 1) vertical spacing with the number on its left 2) top space to superview 3) trailing space to superview and 4) bottom space with the Asked By label. – Kqtr Apr 10 '17 at 10:24