I am trying to create a horizontal axis UIStackView
with 2 views, a label and an image, that would look something like this:
- Blue is the label
- Image is the image
- The frame of the
UIStackView
is the green portion + the blue portion + image
What I am trying to achieve is
What I am trying to achieve:
- The text needs to be bottom aligned to the stackview
- The text can be 1 or more lines
- There may be an image and in that case its dimensions would be fixed at 120 x 120
- If the label grows beyond the height of the image, the image remains bottom aligned
- There may or may not be an image in which case the label should take the full width of the stackview
- The
UIStackView
needs to grow dynamically depending on which is taller, the label or the image
This is my set up in storyboard:
The constraints are as follows:
The edge case I am struggling with is when I have a text that is quite long and needs to wrap, it shrinks the image and it looks like this:
I know one way to solve it would be to change the image constraints from lessThanEqual
to Equal
, however when I do this, the space allocated for the imageview doesn't get removed when the image is nil and prevents the label from taking the full width of the stackview
How could I create the constraints in such a way that
- Makes sure the image at 128x128 when there is an image
- The label does not shrink the image when an image is present
- The label can take the full width of the stackview if the image is set to nil