<!--
{
  "availability" : [
    "macOS: 10.9.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSStackView",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSStackView"
  },
  "title" : "NSStackView"
}
-->

# NSStackView

A view that arranges an array of views horizontally or vertically and updates their placement and sizing when the window size changes.

```
class NSStackView
```

## Overview

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. For more information on using a stack view effectively, see [`NSLayoutConstraint`](/documentation/AppKit/NSLayoutConstraint).

### 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`](/documentation/AppKit/NSStackView/views) array of another stack view.

> Important:
> Do not add views or constraints to a stack view’s private views. A stack view’s private views might change in future versions of macOS and are not guaranteed to be encoded or decoded with the <doc://com.apple.documentation/documentation/Foundation/NSCoder> class.

For more information on [`NSStackView`](/documentation/AppKit/NSStackView), see [Organize Your User Interface with a Stack View](/documentation/AppKit/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 [`userInterfaceLayoutDirection`](/documentation/AppKit/NSView/userInterfaceLayoutDirection) property (inherited from the [`NSView`](/documentation/AppKit/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 [`userInterfaceLayoutDirection`](/documentation/AppKit/NSView/userInterfaceLayoutDirection) method on your stack view instance.

To specify vertical layout, use the [`orientation`](/documentation/AppKit/NSStackView/orientation) property and the [`NSUserInterfaceLayoutOrientation.vertical`](/documentation/AppKit/NSUserInterfaceLayoutOrientation/vertical) constant from the [`NSUserInterfaceLayoutOrientation`](/documentation/AppKit/NSUserInterfaceLayoutOrientation) 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. A view that is hidden, but not detached, remains part of the view hierarchy and continues to participate in Auto Layout, but it is not visible and doesn’t receive input events.

To allow views to detach, set the so-called *clipping resistance* for a stack view to a value lower than its default of [`required`](/documentation/AppKit/NSLayoutConstraint/Priority-swift.struct/required). See the [`setClippingResistancePriority(_:for:)`](/documentation/AppKit/NSStackView/setClippingResistancePriority(_:for:)) method.

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 [`NSStackView.VisibilityPriority`](/documentation/AppKit/NSStackView/VisibilityPriority) enumeration and the [`setVisibilityPriority(_:for:)`](/documentation/AppKit/NSStackView/setVisibilityPriority(_:for:)) method.

To explicitly detach a view from a stack view, call the [`setVisibilityPriority(_:for:)`](/documentation/AppKit/NSStackView/setVisibilityPriority(_:for:)) method with a value of [`notVisible`](/documentation/AppKit/NSStackView/VisibilityPriority/notVisible). To explicitly reattach a view to a stack view, call the same method with a value of [`mustHold`](/documentation/AppKit/NSStackView/VisibilityPriority/mustHold). If you hide a view that belongs to a stack view (by setting the view’s [`isHidden`](/documentation/AppKit/NSView/isHidden) property to <doc://com.apple.documentation/documentation/Swift/true>), the view detaches from the stack view by default. Use the [`detachesHiddenViews`](/documentation/AppKit/NSStackView/detachesHiddenViews) property to change the default behavior.

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 [`NSStackViewDelegate`](/documentation/AppKit/NSStackViewDelegate).

## Topics

### Creating a Stack View

[`init(views:)`](/documentation/AppKit/NSStackView/init(views:))

Creates and returns a stack view with a specified array of views.

### Responding to Stack-Related Changes

[`delegate`](/documentation/AppKit/NSStackView/delegate)

The delegate object for the stack view.

[`NSStackViewDelegate`](/documentation/AppKit/NSStackViewDelegate)

A set of methods you use to respond to a stack view detaching and reattaching views.

### Managing Views in Gravity Areas

[`addView(_:in:)`](/documentation/AppKit/NSStackView/addView(_:in:))

Adds a view to the end of the stack view gravity area.

[`insertView(_:at:in:)`](/documentation/AppKit/NSStackView/insertView(_:at:in:))

Adds a view to a stack view gravity area at a specified index position.

[`setViews(_:in:)`](/documentation/AppKit/NSStackView/setViews(_:in:))

Specifies an array of views for a specified gravity area in the stack view, replacing any previous views in that area.

[`removeView(_:)`](/documentation/AppKit/NSStackView/removeView(_:))

Removes a specified view from the stack view.

[`NSStackView.Gravity`](/documentation/AppKit/NSStackView/Gravity)

The gravity areas available in a stack view.

### Managing the Arranged Subviews

[`addArrangedSubview(_:)`](/documentation/AppKit/NSStackView/addArrangedSubview(_:))

Adds the specified view to the end of the arranged subviews list.

[`insertArrangedSubview(_:at:)`](/documentation/AppKit/NSStackView/insertArrangedSubview(_:at:))

Adds the provided view to the array of arranged subviews at the specified index.

[`removeArrangedSubview(_:)`](/documentation/AppKit/NSStackView/removeArrangedSubview(_:))

Removes the provided view from the stack’s array of arranged subviews.

[`arrangedSubviews`](/documentation/AppKit/NSStackView/arrangedSubviews)

The array of views arranged by the stack view.

### Inspecting a Stack View

[`views`](/documentation/AppKit/NSStackView/views)

The array of views owned by the stack view.

[`views(in:)`](/documentation/AppKit/NSStackView/views(in:))

Returns the array of views in the specified gravity area in the stack view.

[`detachedViews`](/documentation/AppKit/NSStackView/detachedViews)

An array that contains the detached views from all the stack view’s gravity areas.

[`clippingResistancePriority(for:)`](/documentation/AppKit/NSStackView/clippingResistancePriority(for:))

Returns the Auto Layout priority for resisting clipping of views in the stack view when Auto Layout attempts to reduce the stack view’s size.

[`huggingPriority(for:)`](/documentation/AppKit/NSStackView/huggingPriority(for:))

Returns the Auto Layout priority for the stack view to minimize its size to fit its contained views as closely as possible, for a specified user interface axis.

### Configuring the Stack View Layout

[`orientation`](/documentation/AppKit/NSStackView/orientation)

The horizontal or vertical layout direction of the stack view.

[`NSUserInterfaceLayoutOrientation`](/documentation/AppKit/NSUserInterfaceLayoutOrientation)

The stack view layout directions, and user interface axes for hugging priority and clipping resistance.

[`alignment`](/documentation/AppKit/NSStackView/alignment)

The view alignment within the stack view.

[`spacing`](/documentation/AppKit/NSStackView/spacing)

The minimum spacing, in points, between adjacent views in the stack view.

[`useDefaultSpacing`](/documentation/AppKit/NSStackView/useDefaultSpacing)

[`edgeInsets`](/documentation/AppKit/NSStackView/edgeInsets)

The geometric padding, in points, inside the stack view, surrounding its views.

[`hasEqualSpacing`](/documentation/AppKit/NSStackView/hasEqualSpacing)

A Boolean value that indicates whether the spacing between adjacent views should be equal to each other.

[`distribution`](/documentation/AppKit/NSStackView/distribution-swift.property)

The spacing and sizing distribution of stacked views along the primary axis.

[`NSStackView.Distribution`](/documentation/AppKit/NSStackView/Distribution-swift.enum)

### Configuring Views in a Stack View

[`customSpacing(after:)`](/documentation/AppKit/NSStackView/customSpacing(after:))

Returns the custom spacing, in points, between a specified view in the stack view and the view that follows it.

[`setCustomSpacing(_:after:)`](/documentation/AppKit/NSStackView/setCustomSpacing(_:after:))

Specifies the custom spacing, in points, between a specified view and the view that follows it in the stack view.

[`visibilityPriority(for:)`](/documentation/AppKit/NSStackView/visibilityPriority(for:))

Returns the visibility priority for a specified view in the stack view.

[`setVisibilityPriority(_:for:)`](/documentation/AppKit/NSStackView/setVisibilityPriority(_:for:))

Sets the Auto Layout priority for a view to remain attached to the stack view when Auto Layout reduces the stack view’s size.

[`NSStackView.VisibilityPriority`](/documentation/AppKit/NSStackView/VisibilityPriority)

The various Auto Layout priorities for a view in the stack view to remain attached.

[`useDefaultSpacing`](/documentation/AppKit/NSStackView/useDefaultSpacing)

### Configuring Dynamic Behavior for a Stack View

[`detachesHiddenViews`](/documentation/AppKit/NSStackView/detachesHiddenViews)

A Boolean value that indicates whether the stack view removes hidden views from its view hierarchy.

[`setClippingResistancePriority(_:for:)`](/documentation/AppKit/NSStackView/setClippingResistancePriority(_:for:))

Sets the Auto Layout priority for resisting clipping of views in the stack view when Auto Layout attempts to reduce the stack view’s size.

[`setHuggingPriority(_:for:)`](/documentation/AppKit/NSStackView/setHuggingPriority(_:for:))

Sets the Auto Layout priority for the stack view to minimize its size, for a specified user interface axis.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)