2

Having reviewed most of Apple's (paucity) of documentation regarding NSStackView, plus the wise words of the greater Internet, including the collective wisdom of Stackoverflow, a problem remains.

Any NSView I add programmatically to my (IB instantiated) NSStackView, visually locate themselves to the bottom of their respective gravity area. Hence they overlay each other and don't behave like an expected "stack" of views with appropriate spacing.

Each of the gravity areas seem somehow compressed and unable to accommodate any more views. The more I apply NSStackView.addView to a gravity area the slimmer the existing views become.

I would expect NSStackView to naturally expand to accommodate the .size of the embedded views.

Any wise counsel is most appreciated ...

---more details—

I place the StackView into the main window using interface builder. I can then add other views into areas of the stack in interface builder, such as buttons. These stack and layout as expected in IB and when running.

However, when I add or insert any view programmatically to any gravity area their bottom edge is positioned vertically down to the bottom edge of the StackView. This layout behaviour is the same for a vertical or horizontal stack.

Consequently, if I add more than one view programmatically they are sized OK and display but overlap. I am using 'NSBox' objects as views (for now) and they are sized using '.sizeToFit()'. I can see that their '.bounds.height' and 'width' are non zero.

I'm not sure how to set or log the intrinsic content size. That might be the next thing to try.

—no luck—

So, I have now worked rigorously through all permutations of: compression, hugging, (non-zero) intrinsic sizing, constraints, no constraints and various types of NSViews, but ... if I '.addView(...)' or '.insertView(...)' more than once, in any 'NSStackView' gravity area, the subsequent views overlay the previous ones and position themselves on the particular baseline. This is the edge closest to the floor of my office, regardless of the stack orientation.

Any 'NSStackView' and AutoLayout help, either emotional or technical, is most appreciated.

Rojer
  • 21
  • 3
  • Tell us about how the stack view is situated in a view hierarchy and window. Also, what constraints have you applied to the stack view relating it to its container and/or sibling views? What kinds of views are you adding to the stack view? In particular, are they the kind which have intrinsic content size? What constraints, if any, have you added to those views? – Ken Thomases Mar 28 '15 at 20:57
  • `NSBox` has no intrinsic content size. Unless you set explicit width and height constraints on it, or put views within it that have intrinsic or explicit size and relate those views to their container with constraints, the layout is ambiguous and you can get arbitrary results. It's quite likely that the size will be zeroed. – Ken Thomases Mar 29 '15 at 15:26

1 Answers1

2

So it appears all the woes with NSStackView come from its constraints. You must provide constraints fro the subviews in a correct form. The easiest way to do this is to put an object/view with solid constraints within each subview of the stackview.

In Interface Builder when I place a simple NSImageView with size, width, and leading constraints within the NSView that will become a subview of the Stackview, the stack shows properly.

If, however, I only have empty NSViews within my NSStackView, even if styled and assigned frames, the NSView will not show up, or all the views will be stacked against the baseline.

This NSView will NOT display in an NSStackView: Unconstrained NSView

This NSView will display in an NSStackView: Constrained NSView

jtf
  • 125
  • 7
  • 1
    I thought the whole point of the NSStackView was to simplify and add appropriate constraints. Can't you just get away with a width and height constraint and let the gravity / orientation of the NSStackView take care of the rest? – wcochran Aug 16 '17 at 18:08