14

I'm trying to create a relatively simple (!) layout with iOS 9 Stack Views. This is the Storyboard:

enter image description here

With the following layout:

enter image description here

But it's ambiguous: Titles Stack View: Need constraints for: X position or width, Times Stack View: Need constraints for: X position or width. I don't understand why. I've set the horizontal content hugging priority of the Titles Stack View to less than that of the Times Stack View, what else do I need to do?

Robert Atkins
  • 23,528
  • 15
  • 68
  • 97
  • Sometimes, stacks within your view need to know a little more info. In terms of adding constraints to a stack view, I've found it's easiest to work from the outside in. Start at the top and work your way inward. You might need to pin sub-stacks or give them a little more information re: "what to do". As guiding principles, work from the outside inward and "less is more" when it comes to constraints. – Adrian Feb 11 '16 at 15:49
  • Just eyeballing your layout, I'd look at your start time & end time stacks. I'd configure them to fill the stack. It appears there's some space between the end label and the leading edge of the frame...I suspect that's the genesis of your warnings. – Adrian Feb 11 '16 at 15:53
  • I don't think it's that @AdrianB, they're set to "Equal centering" which pushes the `End` label in a bit. Setting them to `Fill Equally` doesn't help. – Robert Atkins Feb 11 '16 at 16:10

2 Answers2

10

My Times Stack View alignment was set to Trailing, setting it to Fill fixed the problem.

(Though Xcode shows some of the views misplaced no matter what I do, but I'm putting this down as a bug)

Robert Atkins
  • 23,528
  • 15
  • 68
  • 97
  • This should be chosen as the answer, as filling the remaining space removes the x-axis constraint issue. – Nitesh Apr 07 '18 at 13:20
6

No need to live with misplaced views or a stack view that doesn't do quite what you want. Try this: Bump the content hugging priority on your "Title 1" label to 252. I bet that will do the trick. If it doesn't, set it back to 251 and tinker a little with the content hugging priority (and maybe compression resistance) on your other labels. Don't go crazy with it. A one-point adjustment to one or two labels should give you the result you want.

  • 2
    Thanks Edward!! To expand a bit, basically, the idea behind this is: UIStackView can have variable size depending on its content (depending on how you setup your constraints), and if that's the case then the child subviews should have a valid, non ambiguous intrinsic content size. I was experiencing this issue when my content views' labels were empty, and setting a defined height constraint for them did the trick! – blastervla Apr 01 '20 at 00:09
  • 1
    I was in this same situation as the question (2 nested vertical StackViews in one horizontal StackView) and setting priorities had no effect to remove the ambiguity – Jerem Lachkar Dec 29 '20 at 02:53