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.
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.
Here is the complete explanation,
If you do not give any height to the Imageview then how it looks like
ImageView grows
If you give height to the Imageview,
Label shrinks to the height of imageview
Put ImageView Inside a view like below,
Now, give proper constraints to the imageview. Now it works properly.
Output:
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:
Constraints:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With