Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 403
AjinkyaSharma Avatar asked Dec 27 '17 07:12

AjinkyaSharma


People also ask

How to set the height of the UIScrollView including the uistackview?

The proper way to set the UIScrollView including UIStackView is to use option #1. Set the width of the UIStackView equal to the UIViewController view. Also add the height constraint to UIStackView marked as removed at build time.

How do I use a vertical stack view in Interface Builder?

This recipe uses a single, vertical stack view to lay out a label, image view, and button. In Interface Builder, start by dragging out a vertical stack view, and add the flowers label, image view, and edit button. Then set up the constraints as shown. In the Attributes inspector, set the following stack view attributes:

How to create a horizontal stack view for a row?

This creates a horizontal stack view for the row. Next, position these rows horizontally, select them, and click the Editor > Embed In > Stack View menu item again. This creates a horizontal stack of rows.

How do you set the height of a custom view?

Custom views’ height will be automatically set in runtime according to the height of the views they contain. As you can see in the gif below, those inner views are empty and no constraints are set. Yet there aren’t any auto layout errors. Views get their height from their placeholders.


1 Answers

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.

like image 193
Bista Avatar answered Sep 21 '22 14:09

Bista