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—</p>

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.

4

1 回答 1

2

所以看起来 NSStackView 的所有问题都来自于它的约束。您必须以正确的形式为子视图提供约束。最简单的方法是在 stackview 的每个子视图中放置一个具有实体约束的对象/视图。

在 Interface Builder 中,当我在将成为 Stackview 子视图的 NSView 中放置一个具有大小、宽度和前导约束的简单 NSImageView 时,堆栈会正确显示。

但是,如果我的 NSStackView 中只有空的 NSView,即使设置了样式并分配了框架,NSView 也不会显示,或者所有视图都将堆叠在基线上。

此 NSView 不会显示在 NSStackView 中: 不受约束的 NSView

此 NSView 将显示在 NSStackView 中: 受约束的 NSView

于 2015-06-27T18:27:33.177 回答