4

I would like to know if it's possible to load a SwiftUI view into an NSView. I was hoping to set the windows content view to aSwiftUI but it seems impossible window?.contentView = view where view is of swiftUI

1 Answer 1

10

If we talk about AppKit which NSView is part of. There is a way to convert SwiftUI view to NSView using NSHostingView like this:

NSHostingView(rootView: yourSwiftUIView)

So, you can write:

window?.contentView = NSHostingView(rootView: view)

If you need the same for UIKit - use UIHostingController in pretty much similar but slightly more complex way:

UIHostingController(rootView: view).view
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.