0

My constraints are not working inside a UITableViewCell, although there are no warnings and the layout is working in interface builder if I change between devices and do rotation.

What did I do:

I created a UITableViewCell called SearchResultCell. I want to layout a button on the right hand side and two labels on the left and the labels should horizontally fill the entire space until the button.

Initially, I just placed the views there and - no matter which constraints I defined - in the simulator all views always appeared at the top left corner.

Then I tried embedding them into stack views. The two labels into a vertical stack view which I then put together with the button into a horizontal stack view. Then I defined constraints such that the outmost stack view should fill the entire superview, i.e. I set bottom, trailing, top, and leading constraints to 0.

My constraints

Again, in interface builder it is always shown correctly:

Layout works correctly in interface builder

However, once launched, these constraints do not seem to be applied.

How it looks like in simulator

The only (slight) improvement is that now the button is beside the label thanks to the stack view. But no matter what I try, I cannot get the constraints working inside this UITableViewCell.

What am I doing wrong here? I don't see it.

Simon
  • 41
  • 5
  • It sounds like you're doing what you need to do... Are you sure you're referencing the correct cell class in cellForRowAt? – DonMag Jun 27 '18 at 15:55
  • I am using dequeueReusableCell with the correct identifier. – Simon Jun 28 '18 at 04:29
  • Take a look at: https://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights. It has very detailed answer for using dynamic(self-sizing) cells. – srvv Jun 28 '18 at 16:37
  • I read that article. Luckily, I do not need self-sizing cells because all cells in my table view will have identical layout and hence the same row height. Another key point is: "don't pin subviews to the cell itself; only to the cell's contentView" - which is exactly what I did, I verified it. So the last thing I could do was to follow the recommendation to "set up your constraints in code", and I did that too by overriding updateConstraints(), still the behavior does not change. I cannot get the stack view to fill the entire content view of my UITableViewCell. – Simon Jun 28 '18 at 18:34

1 Answers1

0

Did a lot of more digging and finally came across this - indeed, it was my problem as well: Autolayout is ignored in Custom UITableViewCell

Simon
  • 41
  • 5