Generic Class

NSHostingView

An AppKit view that hosts a SwiftUI view hierarchy.

Declaration

class NSHostingView<Content> : NSView 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

init(rootView: Content)

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

init?(coder: NSCoder)

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

init(frame: NSRect)

Initializes and returns a newly allocated AppKit view with a specified frame rectangle.

Getting the Root View

var rootView: Content

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

Providing a Contextual Menu

Comparing Views

Hit Testing in the View

Getting Hash Information

var hashValue: Int

The hash value.

func hash(into: inout Hasher)

Hashes the essential components of this value by feeding them into the given hasher.

See Also

AppKit Hosting

class NSHostingController

An AppKit view controller that hosts a set of Swift UI views.

protocol NSViewControllerRepresentable

A wrapper that you use to integrate an AppKit view controller into your SwiftUI interface.

protocol NSViewRepresentable

A wrapper that you use to integrate an AppKit view into your SwiftUI view hierarchy.