NSHostingView
An AppKit view that hosts a SwiftUI view hierarchy.
Declaration
@MainActor @preconcurrency class NSHostingView<Content> where Content : ViewOverview
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
requiresConstraintBasedLayoutuserInterfaceLayoutDirectionisFlippedlayerContentsRedrawPolicyupdateConstraints()layout()safeAreaRegions
Managing keyboard interaction
keyDown(with:)keyUp(with:)performKeyEquivalent(with:)insertText(_:)didChangeValue(forKey:)makeTouchBar()
Responding to mouse events
mouseDown(with:)mouseUp(with:)otherMouseDown(with:)otherMouseUp(with:)rightMouseDown(with:)rightMouseUp(with:)mouseEntered(with:)mouseExited(with:)mouseDragged(with:)mouseMoved(with:)otherMouseDragged(with:)rightMouseDragged(with:)cursorUpdate(with:)
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
viewWillMove(toWindow:)viewDidMoveToWindow()viewDidChangeBackingProperties()viewDidChangeEffectiveAppearance()
Modifying the frame rectangle
intrinsicContentSizesetFrameSize(_:)firstBaselineOffsetFromToplastBaselineOffsetFromBottomsizingOptionsfirstTextLineCenter
Testing for hits
Managing accessibility behaviors
accessibilityFocusedUIElementaccessibilityChildren()accessibilityChildrenInNavigationOrder()accessibilityHitTest(_:)accessibilityRole()accessibilitySubrole()isAccessibilityElement()