<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSStackView/addView(_:in:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSStackView(im)addView:inGravity:"
  },
  "title" : "addView(_:in:)"
}
-->

# addView(_:in:)

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

```
func addView(_ view: NSView, in gravity: NSStackView.Gravity)
```

## Parameters

`view`

The view to add to the specified gravity area.

`gravity`

The gravity area that you are adding the specified view to. Valid values are those in the [`NSStackView.Gravity`](/documentation/AppKit/NSStackView/Gravity) enumeration.

## Discussion

The location of a newly added view depends on the stack view layout direction and, for a horizontal stack view, on user interface language:

- *Horizontal*: A newly added view appears at the trailing edge of the specified gravity area, as determined by the value of the inherited [`userInterfaceLayoutDirection`](/documentation/AppKit/NSView/userInterfaceLayoutDirection) property of the stack view. For a left to right language, a new view appears at the right side of the gravity area.
- *Vertical*: A newly added view appears at the bottom of the specified gravity area.

Calling this method updates the stack view’s layout, which can change the stack view size. As a result, views could detach or clip according to the clipping resistance of the stack view and the visibility priorities of its views.

A view in a detached state is not present in the stack view’s view hierarchy, but it still consumes memory. To respond to detachment and reattachment of views, implement an [`NSStackViewDelegate`](/documentation/AppKit/NSStackViewDelegate) object and assign it to the [`delegate`](/documentation/AppKit/NSStackView/delegate) property.

## See Also

[`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.

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

The horizontal or vertical layout direction of the stack view.

[`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.



---

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)