<!--
{
  "availability" : [
    "macOS: 10.15.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/NSHostingView",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI13NSHostingViewC"
  },
  "title" : "NSHostingView"
}
-->

# NSHostingView

An AppKit view that hosts a SwiftUI view hierarchy.

```
@MainActor @preconcurrency class NSHostingView<Content> where Content : View
```

## Overview

You use `NSHostingView` objects to integrate SwiftUI views into your
AppKit view hierarchies. A hosting view is an
<doc://com.apple.documentation/documentation/AppKit/NSView> object that manages a single
SwiftUI view, which may itself contain other SwiftUI views. Because it is an
<doc://com.apple.documentation/documentation/AppKit/NSView> object, you can integrate it
into your existing AppKit view hierarchies to implement portions of your UI.
For example, you can use a hosting view to implement a custom control.

A hosting view acts as a bridge between your SwiftUI views and your AppKit
interface. During layout, the hosting view reports the content size
preferences of your SwiftUI views back to the AppKit layout system so that
it can size the view appropriately. The hosting view also coordinates event
delivery.

## Topics

### Creating a hosting view

[`init(rootView:)`](/documentation/SwiftUI/NSHostingView/init(rootView:))

Creates a hosting view object that wraps the specified SwiftUI view.

[`init(coder:)`](/documentation/SwiftUI/NSHostingView/init(coder:))

Creates a hosting view object from the contents of the specified
archive.

[`prepareForReuse()`](/documentation/SwiftUI/NSHostingView/prepareForReuse())

### Getting the root view

[`rootView`](/documentation/SwiftUI/NSHostingView/rootView)

The root view of the SwiftUI view hierarchy managed by this view
controller.

### Configuring the view layout behavior

[`requiresConstraintBasedLayout`](/documentation/SwiftUI/NSHostingView/requiresConstraintBasedLayout)

[`userInterfaceLayoutDirection`](/documentation/SwiftUI/NSHostingView/userInterfaceLayoutDirection)

[`isFlipped`](/documentation/SwiftUI/NSHostingView/isFlipped)

[`layerContentsRedrawPolicy`](/documentation/SwiftUI/NSHostingView/layerContentsRedrawPolicy)

[`updateConstraints()`](/documentation/SwiftUI/NSHostingView/updateConstraints())

[`layout()`](/documentation/SwiftUI/NSHostingView/layout())

[`safeAreaRegions`](/documentation/SwiftUI/NSHostingView/safeAreaRegions)

The safe area regions that this view controller adds to its view.

### Managing keyboard interaction

[`keyDown(with:)`](/documentation/SwiftUI/NSHostingView/keyDown(with:))

Called when the user presses a key on the keyboard while this view is in
the responder chain.

[`keyUp(with:)`](/documentation/SwiftUI/NSHostingView/keyUp(with:))

Called when the user releases a key on the keyboard while this view is
in the responder chain.

[`performKeyEquivalent(with:)`](/documentation/SwiftUI/NSHostingView/performKeyEquivalent(with:))

[`insertText(_:)`](/documentation/SwiftUI/NSHostingView/insertText(_:))

[`didChangeValue(forKey:)`](/documentation/SwiftUI/NSHostingView/didChangeValue(forKey:))

[`makeTouchBar()`](/documentation/SwiftUI/NSHostingView/makeTouchBar())

### Responding to mouse events

[`mouseDown(with:)`](/documentation/SwiftUI/NSHostingView/mouseDown(with:))

[`mouseUp(with:)`](/documentation/SwiftUI/NSHostingView/mouseUp(with:))

[`otherMouseDown(with:)`](/documentation/SwiftUI/NSHostingView/otherMouseDown(with:))

[`otherMouseUp(with:)`](/documentation/SwiftUI/NSHostingView/otherMouseUp(with:))

[`rightMouseDown(with:)`](/documentation/SwiftUI/NSHostingView/rightMouseDown(with:))

[`rightMouseUp(with:)`](/documentation/SwiftUI/NSHostingView/rightMouseUp(with:))

[`mouseEntered(with:)`](/documentation/SwiftUI/NSHostingView/mouseEntered(with:))

[`mouseExited(with:)`](/documentation/SwiftUI/NSHostingView/mouseExited(with:))

[`mouseDragged(with:)`](/documentation/SwiftUI/NSHostingView/mouseDragged(with:))

[`mouseMoved(with:)`](/documentation/SwiftUI/NSHostingView/mouseMoved(with:))

[`otherMouseDragged(with:)`](/documentation/SwiftUI/NSHostingView/otherMouseDragged(with:))

[`rightMouseDragged(with:)`](/documentation/SwiftUI/NSHostingView/rightMouseDragged(with:))

[`cursorUpdate(with:)`](/documentation/SwiftUI/NSHostingView/cursorUpdate(with:))

### Responding to touch events

[`touchesBegan(with:)`](/documentation/SwiftUI/NSHostingView/touchesBegan(with:))

[`touchesCancelled(with:)`](/documentation/SwiftUI/NSHostingView/touchesCancelled(with:))

[`touchesEnded(with:)`](/documentation/SwiftUI/NSHostingView/touchesEnded(with:))

[`touchesMoved(with:)`](/documentation/SwiftUI/NSHostingView/touchesMoved(with:))

### Responding to gestures

[`magnify(with:)`](/documentation/SwiftUI/NSHostingView/magnify(with:))

[`rotate(with:)`](/documentation/SwiftUI/NSHostingView/rotate(with:))

[`scrollWheel(with:)`](/documentation/SwiftUI/NSHostingView/scrollWheel(with:))

### Handling drag and drop

[`validRequestor(forSendType:returnType:)`](/documentation/SwiftUI/NSHostingView/validRequestor(forSendType:returnType:))

### Providing a context menu

[`menu(for:)`](/documentation/SwiftUI/NSHostingView/menu(for:))

### Responding to actions

[`responds(to:)`](/documentation/SwiftUI/NSHostingView/responds(to:))

[`forwardingTarget(for:)`](/documentation/SwiftUI/NSHostingView/forwardingTarget(for:))

[`doCommand(by:)`](/documentation/SwiftUI/NSHostingView/doCommand(by:))

### Configuring the responder behavior

[`acceptsFirstResponder`](/documentation/SwiftUI/NSHostingView/acceptsFirstResponder)

[`needsPanelToBecomeKey`](/documentation/SwiftUI/NSHostingView/needsPanelToBecomeKey)

### Managing the view hierarchy

[`viewWillMove(toWindow:)`](/documentation/SwiftUI/NSHostingView/viewWillMove(toWindow:))

[`viewDidMoveToWindow()`](/documentation/SwiftUI/NSHostingView/viewDidMoveToWindow())

[`viewDidChangeBackingProperties()`](/documentation/SwiftUI/NSHostingView/viewDidChangeBackingProperties())

[`viewDidChangeEffectiveAppearance()`](/documentation/SwiftUI/NSHostingView/viewDidChangeEffectiveAppearance())

### Modifying the frame rectangle

[`intrinsicContentSize`](/documentation/SwiftUI/NSHostingView/intrinsicContentSize)

[`setFrameSize(_:)`](/documentation/SwiftUI/NSHostingView/setFrameSize(_:))

[`firstBaselineOffsetFromTop`](/documentation/SwiftUI/NSHostingView/firstBaselineOffsetFromTop)

[`lastBaselineOffsetFromBottom`](/documentation/SwiftUI/NSHostingView/lastBaselineOffsetFromBottom)

[`sizingOptions`](/documentation/SwiftUI/NSHostingView/sizingOptions)

The options for how the hosting view creates and updates constraints
based on the size of its SwiftUI content.

[`firstTextLineCenter`](/documentation/SwiftUI/NSHostingView/firstTextLineCenter)

### Testing for hits

[`hitTest(_:)`](/documentation/SwiftUI/NSHostingView/hitTest(_:))

### Managing accessibility behaviors

[`accessibilityFocusedUIElement`](/documentation/SwiftUI/NSHostingView/accessibilityFocusedUIElement)

[`accessibilityChildren()`](/documentation/SwiftUI/NSHostingView/accessibilityChildren())

[`accessibilityChildrenInNavigationOrder()`](/documentation/SwiftUI/NSHostingView/accessibilityChildrenInNavigationOrder())

[`accessibilityHitTest(_:)`](/documentation/SwiftUI/NSHostingView/accessibilityHitTest(_:))

[`accessibilityRole()`](/documentation/SwiftUI/NSHostingView/accessibilityRole())

[`accessibilitySubrole()`](/documentation/SwiftUI/NSHostingView/accessibilitySubrole())

[`isAccessibilityElement()`](/documentation/SwiftUI/NSHostingView/isAccessibilityElement())

### Bridging with SwiftUI

[`sceneBridgingOptions`](/documentation/SwiftUI/NSHostingView/sceneBridgingOptions)

The options for which aspects of the window will be managed by this
hosting view.



---

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)