user86516512
user86516512

Reputation: 445

Label's height behaving strangely when put inside UIView in a stack view

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?

Upvotes: 1

Views: 2169

Answers (1)

mlch911
mlch911

Reputation: 21

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.

Upvotes: 1

Related Questions