Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal stackview label grow with a fixed height element

I have a horizontal stackview with a UIImageView (1:1 Aspect Rati, Height 32) and a label. Since it has a height the stackView cuts off my label on the right not letting me have more than one line.

enter image description here

I want the Horizontal StackView grow in height as the labels need to display its own text or use the UIImageView height if its bigger.

enter image description here

like image 408
Godfather Avatar asked Aug 22 '18 10:08

Godfather


2 Answers

Here is the complete explanation,

If you do not give any height to the Imageview then how it looks like

enter image description here

ImageView grows

If you give height to the Imageview,

enter image description here

Label shrinks to the height of imageview

Solution

Put ImageView Inside a view like below,

enter image description here

Now, give proper constraints to the imageview. Now it works properly.

Output:

enter image description here

like image 104
dahiya_boy Avatar answered Nov 14 '22 23:11

dahiya_boy


GitHub Repo: Source Code Check the code inside FirstViewController

Explanation:

Step 1: Change the constraint of StackView, such that height >= minValue value. After that it's height will auto adjust depending on the height of the UILabel.

Step 2: Call sizeToFit method of the UILabel after you set it's text. That will change the frame of the UILabel.

lable.text = "SomeText Here. It will expalnd the StackView.omeText Here. It will expalnd the StackView.omeText Here. It will expalnd the StackView.omeText Here. It will expalnd the StackView.omeText Here. It will expalnd the StackView.omeText Here. It will expalnd the StackView.omeText Here. It will expalnd the StackView.omeText Here. It will expalnd the StackView."
lable.sizeToFit()

Step 3: Set the Scale Mode of image to Aspect Fit, and set it's height and width constraint.

Step 4: Set the alignment of Stackview as per the requirement, I have set set it to top to make the image and Label to stay at the top.

Final Result:

StackViewLayout with adjustable height

Constraints:

enter image description here

like image 43
Deep Arora Avatar answered Nov 15 '22 00:11

Deep Arora