<!--
{
  "documentType" : "article",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/view-hierarchy",
  "metadataVersion" : "0.1.0",
  "role" : "collectionGroup",
  "title" : "View Hierarchy"
}
-->

# View Hierarchy

Manage the subviews, superview, and window of a view and respond to notifications when the view hierarchy changes.

## Topics

### Getting the Related Objects

[`var superview: NSView?`](/documentation/AppKit/NSView/superview)

The view that is the parent of the current view.

[`var subviews: [NSView]`](/documentation/AppKit/NSView/subviews)

The array of views embedded in the current view.

[`var window: NSWindow?`](/documentation/AppKit/NSView/window)

The view’s window object, if it is installed in a window.

[`var opaqueAncestor: NSView?`](/documentation/AppKit/NSView/opaqueAncestor)

The view’s closest opaque ancestor, which might be the view itself.

[`func isDescendant(of: NSView) -> Bool`](/documentation/AppKit/NSView/isDescendant(of:))

Returns a Boolean value that indicates whether the view is a subview of the specified view.

[`func ancestorShared(with: NSView) -> NSView?`](/documentation/AppKit/NSView/ancestorShared(with:))

Returns the closest ancestor shared by the view and another specified view.

[`var enclosingMenuItem: NSMenuItem?`](/documentation/AppKit/NSView/enclosingMenuItem)

The menu item containing the view or any of its superviews in the view hierarchy.

[`var enclosingScrollView: NSScrollView?`](/documentation/AppKit/NSView/enclosingScrollView)

The nearest ancestor scroll view that contains the current view.

### Adding and Removing Subviews

[`func addSubview(NSView)`](/documentation/AppKit/NSView/addSubview(_:))

Adds a view to the view’s subviews so it’s displayed above its siblings.

[`func addSubview(NSView, positioned: NSWindow.OrderingMode, relativeTo: NSView?)`](/documentation/AppKit/NSView/addSubview(_:positioned:relativeTo:))

Inserts a view among the view’s subviews so it’s displayed immediately above or below another view.

[`func removeFromSuperview()`](/documentation/AppKit/NSView/removeFromSuperview())

Unlinks the view from its superview and its window, removes it from the responder chain, and invalidates its cursor rectangles.

[`func removeFromSuperviewWithoutNeedingDisplay()`](/documentation/AppKit/NSView/removeFromSuperviewWithoutNeedingDisplay())

Unlinks the view from its superview and its window and removes it from the responder chain, but does not invalidate its cursor rectangles to cause redrawing.

[`func replaceSubview(NSView, with: NSView)`](/documentation/AppKit/NSView/replaceSubview(_:with:))

Replaces one of the view’s subviews with another view.

[`func sortSubviews((NSView, NSView, UnsafeMutableRawPointer?) -> ComparisonResult, context: UnsafeMutableRawPointer?)`](/documentation/AppKit/NSView/sortSubviews(_:context:))

Orders the view’s immediate subviews using the specified comparator function.

### Responding to View-Related Notifications

[`func didAddSubview(NSView)`](/documentation/AppKit/NSView/didAddSubview(_:))

Overridden by subclasses to perform additional actions when subviews are added to the view.

[`func viewDidMoveToSuperview()`](/documentation/AppKit/NSView/viewDidMoveToSuperview())

Informs the view that its superview has changed (possibly to `nil`).

[`func viewDidMoveToWindow()`](/documentation/AppKit/NSView/viewDidMoveToWindow())

Informs the view that it has been added to a new view hierarchy.

[`func viewWillMove(toSuperview: NSView?)`](/documentation/AppKit/NSView/viewWillMove(toSuperview:))

Informs the view that its superview is about to change to the specified superview (which may be `nil`).

[`func viewWillMove(toWindow: NSWindow?)`](/documentation/AppKit/NSView/viewWillMove(toWindow:))

Informs the view that it’s being added to the view hierarchy of the specified window object (which may be `nil`).

[`func willRemoveSubview(NSView)`](/documentation/AppKit/NSView/willRemoveSubview(_:))

Overridden by subclasses to perform additional actions before subviews are removed from the view.

### Identifying Views by Tag

[`func viewWithTag(Int) -> NSView?`](/documentation/AppKit/NSView/viewWithTag(_:))

Returns the view’s nearest descendant (including itself) with a specific tag, or `nil` if no subview has that tag.

[`var tag: Int`](/documentation/AppKit/NSView/tag)

The view’s tag, which is an integer that you use to identify the view within your app.



---

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)