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

AjinkyaSharma
  • 1,870
  • 1
  • 16
  • 26

3 Answers3

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.

Bista
  • 7,869
  • 3
  • 27
  • 55
  • Xcode behaved unexpecdly and my Stack views went hidden when i set line to 0. Don't worry runtime will show corect output. – Bista Dec 27 '17 at 08:20
  • 1
    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. – AjinkyaSharma Dec 27 '17 at 08:44
  • Better set the Lines = 0 programatically :) – Bista Dec 27 '17 at 10:27
  • 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. – AjinkyaSharma Dec 27 '17 at 11:07
  • "Lines = 0 programatically" breaks UI? Table or Collection Cell? – Bista Dec 27 '17 at 11:36
  • 1
    TableViewCell. I ended up using Vertical stack view of UIViews, Replaced the horizontal Stack view with UIView consisting of Labels. – AjinkyaSharma Dec 28 '17 at 02:02
0

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

Kathiresan Murugan
  • 2,783
  • 3
  • 23
  • 44
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.

Shourob Datta
  • 1,886
  • 22
  • 30