UIStackView inside UICollectionViewCell autolayout issues after running on Xcode 10 beta
Asked Answered
S

1

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

Seeseebeck answered 26/8, 2018 at 8:5 Comment(4)
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.Rubenstein
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.Mullins
Nope, never managed to fix it unfortunatelySeeseebeck
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.Ewaewald
C
45

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

Circumrotate answered 17/10, 2018 at 13:37 Comment(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.Toot

© 2022 - 2024 — McMap. All rights reserved.