1

I have a collection view cell. Inside this cell, I have a stack view with two labels. One is static height and one is dynamic height.

If my hierarchy is as follows:

- Stack view (distribution = fill)
    - Label (vertical hugging = 250)
    - Label (vertical hugging = 249)

It sets the height fine for each cell:

enter image description here

enter image description here

But when I place the label inside a UIView:

- Stack view (distribution = fill)
    - Label (vertical hugging = 250)
    - UIView (vertical hugging = 249)
        - Label (top, right, bottom, left constraints = superview)

It (most of the time) sets the height incorrectly:

This appears as one line when it should appear as multiple:

enter image description here

Adds weird padding above and below the label:

enter image description here

What's going on here?

user86516512
  • 445
  • 4
  • 13
  • Do not change vertical hugging. Keep it as a default and just set >= relation to the bottom constraint of the stackview – Raja Kishan Feb 22 '21 at 05:43
  • @RajaKishan Sorry, bit confused. The UIView's bottom constraint should be >= 0 of the stackview? – user86516512 Feb 22 '21 at 06:15
  • It doesn’t look like “weird padding” to me. It looks to me like it has the height of the first screen shot. – matt Feb 22 '21 at 06:16
  • @matt I had that thought as well. What would cause that? – user86516512 Feb 22 '21 at 06:26
  • You have not shown your collection view code. But in general I would suggest considering my answer here: https://stackoverflow.com/a/51585910/341994 Use a Composable layout, or give up on the idea of self sizing collection view cells. – matt Feb 22 '21 at 06:27

1 Answers1

1

It's the stackView that decide how height these two label should be.

It's not autolayout like you said. The stackView wouldn't change height whether the hight of these labels are right or not.

What you want to do is to auto-sizing the stackView by those two labels. You should use a UIView instead of UIStackView. UIStackView is a view that auto-sizing its subviews, not the opposite way.

You can write a UIView. Add those two label inside it. And set the constraints of two labels. And this view will be what you want if you don't put any constrains on it.

mlch911
  • 21
  • 3