Search code examples
iosswiftxcodeautolayout

How to maintain bottom space when image and growing label are being used in parallel in swift?


I have 3 elements, imageview, label and a view. I want to maintain the eqaul space (15) for bottom view. Layout is as : Imageview then label and at the bottom, there is a view. I have defined the top constraints of the view >=15 from the imageview, 84 from label but I want to push down the view when the label grows beyond the imgeview. Now what is happening the view is pushing down when the lable gets on new line and there is more than 15pt space between label and the bottom view which I don't want. enter image description here


Solution

  • Probably the easiest way is to put the image view and label in a horizontal stack view, with Alignment set to Top:

    enter image description here

    enter image description here

    The stack view is constrained at 16-pts Top / Leading / Trailing, and 15-pts to the "blue view."

    At run-time, with different amounts of text in the label:

    enter image description here

    enter image description here

    enter image description here