Dynamic height for a horizontal UIStackview
Asked Answered
S

3

5

I have a horizontal StackView with distribution as "fill equally". It has 3 labels. I want one of the labels to have dynamic height. When I set the number of number of lines to 0 for that label, it ends up breaking the constraint the spacing constraint at runtime.

All these horizontal stackviews make a one vertical stackview. I have tried setting lower vertical hugging priority and higher resistance priority to the multiline label

Also it behaves like this in the XIB

Before changing the number of lines enter image description here

After changing the number of lines enter image description here

Slather answered 27/12, 2017 at 7:3 Comment(3)
Have to set the Content Compression Resistance and Hugging Priority?Lode
Desired output? imgur.com/a/tWi2MLode
Yes, I need the same output.Slather
L
7

You do not need to set Content Hugging and Compression resistance property to achieve that effect.

  1. Horizontal Stack Views: Set Alignment "Fill" & Distribution "Fill Equally"

  2. Vertical Stack View: Set Alignment "Fill" & Distribution "Fill"

  3. Set Label Lines to 0.

  4. Set Top, Bottom, Leading Constraint for Vertical Stack View.

Lode answered 27/12, 2017 at 8:10 Comment(6)
Xcode behaved unexpecdly and my Stack views went hidden when i set line to 0. Don't worry runtime will show corect output.Lode
Exactly, same thing happened with me,(Xib goes blank), I have done the same things as you mentioned, because that is how you do it. :p it was the obvious way to proceed.. Anyway, I'll try freshly, and if it works as expected, I'll upvote your answer or write another one.Slather
Better set the Lines = 0 programatically :)Lode
Doesn't work, basically, this whole thing is in a cell, and I want that cell to auto resize, but all it does it shrinks and breaks the UI.Slather
"Lines = 0 programatically" breaks UI? Table or Collection Cell?Lode
TableViewCell. I ended up using Vertical stack view of UIViews, Replaced the horizontal Stack view with UIView consisting of Labels.Slather
D
0

Set all constraints for UIStackView. Update the UIStackview height constraints constant value based on UILabel text height

Derick answered 27/12, 2017 at 7:25 Comment(0)
S
0

Not Directly answering the question rather on how to control the dynamic height of UIStackView.

  1. Vertical Stack View. Make it free of height like no bottom constraints or no height constraints.
  2. Contents(like view) are fixed height.
  3. Both Alignment & Distribution of UIStackView will "fill"

now show or hide the contents then Stactview height will dynamically change.

Scrape answered 13/11, 2020 at 17:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.