Horizontal stackview label grow with a fixed height element
Asked Answered
P

2

7

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

Pathognomy answered 22/8, 2018 at 10:21 Comment(8)
Show me your view hierarchy and constraints. BTW I dont think it is good to use stackBrack
updated @BrackPathognomy
it seems 2 elements inside the horizontal stackview can't have different heightsPathognomy
Use Bottom Constraint too For StackviewScarabaeid
no, i want the stackview to grow as its content. @SagarShirbhatePathognomy
Can you check this project I've prepared just now? dropbox.com/s/dehizstq39oggh4/Deneme.zip?dl=0Serotonin
@Serotonin already checked. It doesn't work. If you want your imageView height to 32 as i need to be, the label on the right also collapses.Pathognomy
@Pathognomy It Will Work As I see an answer Bottom constraint is also added and its simple :)Scarabaeid
B
12

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

Brack answered 22/8, 2018 at 10:41 Comment(11)
so now you need to give constraints horizontally cause neither the imageview and label have those, isnt? But yeah thats exactly the problem.Pathognomy
@Pathognomy the label isn't inside the view. Only the imageIves
you lost the easy setup of the horizontal stack view, in fact theres no need for that since you just put everything inside a uiview, not just the imageiview, also the labelPathognomy
can you share the project?Pathognomy
@Pathognomy I didnt prepared any project for this, it is simple, instead of imageview first take UIView and then put a ImageView in it & give constraints. Thats allBrack
@Pathognomy I've edited to include an image showing the new constraints you'll need for the imageview and the uiviewIves
@Pathognomy add leading, trailing, top and bottom constraints of the imageView to the view of zero. But be sure to change the bottom constraint to >= relationshipIves
@SylvanDAsh no need of the bottom >= .Brack
@SylvanDAsh yeah, the >= was the thing i missed. With this it worksPathognomy
@Brack the bottom >= is necessary so that the label's height can adjust the overall height of the stackviewIves
@SylvanDAsh Check my constraints -> i.sstatic.net/yHDna.pngBrack
B
6

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

Bananas answered 22/8, 2018 at 10:29 Comment(9)
wich constraint?Pathognomy
heightConstaint on the UIStackView.Bananas
i added a height constraint to the stackView, to be greater than or equal min height (32) but still doesnt grow.Pathognomy
@DeepArora But it increases the height of Imageview also.Brack
@Pathognomy Remove the height of ImageView and If there is any height of StackView then make it >= . Now checkBrack
I dont want to remove the height o the ImageView, otherwise it grows also.Pathognomy
can you share this project?Pathognomy
Added the GitHub link, let me know if that still doesn't solve your problem, and if it does, please access my answer. Thanks.Bananas
Beautifully explained, worked perfectly. Thank youMeasures

© 2022 - 2024 — McMap. All rights reserved.