20

My stack view contains an image and label to display some tasks. They were all left aligned on multiple rows.

I didn't have any issue running on Xcode 9, but when I run on Xcode 10 beta 6, I get some autolayout issues, I always get errors like for the stack view:

Need constraint for X position
Need constraint for Y position

My current constraints for stack view are:

Leading edge to cell - 5
Trailing edge to cell - 5
Bottom edge to cell - 2
Top edge to cell - 2

So I don't see what changed to complain about this, for me it's obvious that I have the X and Y set already.

Here is two screenshots with how it looks when it runs (label is truncated) and the storyboard:

enter image description here

enter image description here

Adrian
  • 19,440
  • 34
  • 112
  • 219
  • If I remember right, in iOS10 labels don't have a frame. Try to do this: yourLabel.sizeToFit() it will make a frame to your label. – Alex Kolovatov Aug 26 '18 at 14:00
  • Did you ever figure out what was going on with this? I am getting the same issue and I assume it is a bug, but I could be wrong. – Kush Oct 25 '18 at 18:11
  • Nope, never managed to fix it unfortunately – Adrian Oct 26 '18 at 13:37
  • Add an empty view (UIView and constraint it to edges of the superview) inside the cell and then add the stack view inside that view. Hope this helps. – soumil Feb 22 '19 at 22:03

1 Answers1

45

Embed your Stack View in a UIView. Also give the view constraints relative to UICollectionViewCell to make it behave like ContentView in UITableViewCell.

Jeet Gandhi
  • 577
  • 4
  • 5
  • 1
    can you explain why this fixes the issue? UICollectionViewCell has a "contentView" inside them. And adding the constraints to that view does not help with this issue. – Xaxxus Aug 15 '22 at 20:08