Contents

NSHostingView

An AppKit view that hosts a SwiftUI view hierarchy.

Declaration

@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 NSView object that manages a single SwiftUI view, which may itself contain other SwiftUI views. Because it is an 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

Getting the root view

Configuring the view layout behavior

Managing keyboard interaction

Responding to mouse events

Responding to touch events

Responding to gestures

Handling drag and drop

Providing a context menu

Responding to actions

Configuring the responder behavior

Managing the view hierarchy

Modifying the frame rectangle

Testing for hits

Managing accessibility behaviors

Bridging with SwiftUI

Initializers

Instance Properties

Instance Methods

See Also

Displaying SwiftUI views in AppKit