简体   繁体   中英

Is there a way to load swiftUI view into NSView?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM