问题
I am trying to see if I can easily use a UIStackView to create multi line labels and also display an image.
Here are my current constraints.
When I run it I get the following:
If I remove the imageview then the multi line works. Somehow UIImageView width and height is messing up everything. What am I doing wrong?
回答1:
Change height and width of the imageView to give proper look and don't give fixed height for the stackview, only give top, bottom, leading and trailing.
THis is how it will look like
回答2:
StackView
does not really work well with constraints, as they kind of destroy the purpose of stack views. Since the vertical inner stackView
is inside a horizontal stackView
with the imageView
. When you give imageView
hard constraints, in order to satisfy these constraints and keep the height & width values of stackViews
at same level with imageView
and inner stackView
, the inner stackView
cannot expand and it's height and you cannot get the behaviour you want. So as also mentioned in the comments, it is better to achieve your design without stackViews
.
PS: I'm not saying stackViews cannot be used with constraints in their subViews. It can be done, and sometimes allow programmers to successfully achieve their goal, but you will always get debugger warnings due to conflicting constraints about the views with constraints inside a stackView.
来源:https://stackoverflow.com/questions/54028617/multi-line-labels-and-an-image-with-uistackview