AjinkyaSharma
AjinkyaSharma

Reputation: 1979

Dynamic height for a horizontal UIStackview

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

Upvotes: 5

Views: 17462

Answers (3)

Shourob Datta
Shourob Datta

Reputation: 2072

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.

Upvotes: 0

Bista
Bista

Reputation: 7893

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.

Upvotes: 7

Kathiresan Murugan
Kathiresan Murugan

Reputation: 2962

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

Upvotes: 0

Related Questions