Skip to content

Instantly share code, notes, and snippets.

@laevandus
Created September 18, 2022 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laevandus/8c2b97163af31b087e15ae9e032aef14 to your computer and use it in GitHub Desktop.
Save laevandus/8c2b97163af31b087e15ae9e032aef14 to your computer and use it in GitHub Desktop.
extension View {
func embeddedInHostingController() -> UIHostingController<some View> {
let provider = ViewControllerProvider()
let hostingAccessingView = environmentObject(provider)
let hostingController = UIHostingController(rootView: hostingAccessingView)
provider.viewController = hostingController
return hostingController
}
}
final class ViewControllerProvider: ObservableObject {
fileprivate(set) weak var viewController: UIViewController?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment