Change the root view of UIHostingController in SwiftUI

前端 未结 5 922
情歌与酒
情歌与酒 2020-12-03 07:19

For a new SwiftUI iOS app, I do the following in the SceneDelegate

if let windowScene = scene as? UIWindowScene {
    let window =          


        
5条回答
  •  温柔的废话
    2020-12-03 07:59

    let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene
    
    if let windowScenedelegate = scene?.delegate as? SceneDelegate {
       let window = UIWindow(windowScene: scene!)
       window.rootViewController = UIHostingController(rootView:ContentView())
       windowScenedelegate.window = window
       window.makeKeyAndVisible()
    }
    

    By using this we can change the rootView in any button click by implementing the above code.

提交回复
热议问题