我在 NSView 中有一个 NSTabView。该 NSView 在一个 NSClipView 中,而后者又在一个 NSScrollView 中。它看起来像这样(绿色为 NSTabView,红色为 NSView):
如您所见,NSTabView 的内容被剪裁,并且没有滚动条出现(因为视图没有扩展到窗口之外)。
我怎样才能让 NSTabView 占用尽可能多的空间(不剪掉),并用它扩展 NSView?然后,NSScrollView 可以处理过度生长的 NSView 的滚动。
由于我的内容是动态变化的,我不想为 NSTabView 的超级视图的宽度和高度设置一些硬值。
这只是其中的一部分;这是现在的整体层次结构:
I want the NSTabView's superview to be scrollable instead of clipping out, like this:
The setup I'll describe is for an NSTabView
that will pin to the top, left, and right sides of the scroll view. Note the NSTabView
could be replaced with any other NSView
, the setup is the same.
Starting with you putting a scroll view into the xib/storyboard, you'll have NSScrollView -> NSClipView -> NSView (document view)
. Constrain the NSScrollView to the edges of the window. Drop your NSTabView
onto the NSView
instance. Add constraints so that your NSTabView
edge constraints equal the NSView
and define a height constraint either explicitly or implicitly with other content inside the tab view that defines it.
Personally I like to change the NSView
instance (document view) layout to use constraints, by default it uses autoresizing masks and this makes it difficult to keep it in sync with the NSTabView. We want the document view to be pinned to the top, left, and right sides of the scroll view. The size of this view is what determines the scrollable region so we want it to be the same size as the NSTabView so the height of the tab view will determine the scrollable area.
To change this, select the document view, and under the Size Inspector we want to change the "Layout" type to "Automatic".
Lastly, add constraints to the top, left, and right and you should be good to go.
If you want the scroll view to start at the top rather than the bottom, you should subclass the document view and override isFlipped
:
class FlippedView: NSView {
override var isFlipped: Bool { true }
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句