A view that arranges an array of views horizontally or vertically and that automatically updates their placement and sizing when the window size changes.
- macOS 10.9+
A stack view employs Auto Layout (the system’s constraint-based layout feature) to arrange and align an array of views according to your specification. To use a stack view effectively, you need to understand the basics of Auto Layout constraints as described in Auto Layout Guide.
Basic Features of Stack Views
A stack view supports vertical and horizontal layouts and interacts dynamically with window resizing and Cocoa animations. You can easily reconfigure the contents of a stack view at runtime. That is, after you create and configure a stack view in Interface Builder, you can add or remove views dynamically without explicitly working with layout constraints. For example, if you configure a stack view with three checkboxes and dynamically add a fourth, the stack view automatically adds constraints as needed, according to the stack view’s configuration. The new checkbox gains dynamic layout configuration from the stack view.
Stack views are nestable: a stack view is a valid element in the
views array of another stack view.
For more information on NSStackView, see Organize Your User Interface with a Stack View.
Layout Direction and Gravity Areas
A stack view has three so-called gravity areas that each identify a section of the stack view’s layout. A horizontal stack view, which is the default type, has a leading, a center, and a trailing gravity area. The ordering of these areas depends on the value of the stack view’s
user property (inherited from the
NSView class). In a left to right language, the leading gravity area in a horizontal stack view is on the left. To enforce a left to right layout independently of language, explicitly set the layout direction by calling the inherited
user method on your stack view instance.
To specify vertical layout, use the
orientation property and the
NSUser constant from the
NSUser enumeration. In a vertical stack view, the gravity areas always are top, center, and bottom.
View Detachment and Hiding
A stack view can automatically detach and reattach its views in response to layout changes, such as window resizing performed by the user, or resizing/repositioning of another view in the same view hierarchy. A view in a detached state is not present in the stack view’s view hierarchy, but it still consumes memory.
To allow views to detach, set the so-called clipping resistance for a stack view to a value lower than its default of NSLayoutPriorityRequired. See the
You can influence which views detach first (and reattach last). Do this by setting the so-called visibility priority for each view whose detachment order you want to specify. A view with a lower visibility priority detaches before one with a higher priority, and reattaches after it. See the
NSStack enumeration and the
To explicitly detach a view from a stack view, call the
set method with a value of
NSStack. To explicitly reattach a view to a stack view, call the same method with a value of
NSStack. If you hide a view that belongs to a stack view (by calling the view’s
hidden method with a value of
YES), the view does not detach from the stack view. It is no longer visible and it no longer receives input events, but it remains part of the view hierarchy and continues to participate in Auto Layout.
The system calls a stack view delegate method when a view is about to be detached and when a view has been reattached, giving you the opportunity to run code at those times. See