1

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. enter image description here

When I run it I get the following:

enter image description here

If I remove the imageview then the multi line works. Somehow UIImageView width and height is messing up everything. What am I doing wrong?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
john doe
  • 9,220
  • 23
  • 91
  • 167
  • Is your stack view horizontal and embedded in a UITableView cell with another embedded vertical stack view inside of it? I think there's a lot of extra hands in the pot when you're laying out. I would likely try and lose the 2nd nested vertical stack and calculate the height of the cells instead. – Dare Jan 03 '19 at 19:35
  • Yes exactly. I have a UITableViewCell. Inside the cell I have a parent stackview. Inside the stackview I have another vertical stackview which contains the label. In the parent stackview I also have uiimageview. If I loose the vertical stackview then uilabels will not align vertically. – john doe Jan 03 '19 at 19:40
  • 1
    In my personal opinion, both of those stack views are unnecessary. I think you can accomplish what you want with layout constraints and add your subviews directly to the cell. – Dare Jan 03 '19 at 19:42
  • Yes! I was able to accomplish that easily without UIStackView. I just wanted to see if it was easier when using UIStackView or not. I guess not. – john doe Jan 03 '19 at 19:43

2 Answers2

3

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.

emrepun
  • 2,496
  • 2
  • 15
  • 33
3

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. Xcode Screenshot

THis is how it will look like Screenshot

Viren Patel
  • 128
  • 1
  • 11