SwiftUI Beta 3黑屏

Dav*_*d L 10 swift swiftui xcode11

我刚刚转换为Beta 3,以前使用的SwiftUI代码现在正在呈现纯黑屏幕。Beta 3是否发生了变化,从而导致了此问题?有解决方案吗?

场景委托代码:

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

        // Use a UIHostingController as window root view controller


    let window = UIWindow(frame: UIScreen.main.bounds)


     window.rootViewController = UIHostingController(rootView: ContentView())
     self.window = window
     window.makeKeyAndVisible()

    }
Run Code Online (Sandbox Code Playgroud)

Dav*_*d L 21

场景Delgate的Beta 3工作版本:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

    // Use a UIHostingController as window root view controller
    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(rootView: ContentView())
        self.window = window
        window.makeKeyAndVisible()
    }
}
Run Code Online (Sandbox Code Playgroud)

归功于Reddit帖子的答案。


为了澄清,使用的beta 1 UIWindow(frame: ...)现在已更改为UIWindow(windowScene: ...)。现在,传递的参数是当前场景,并类型转换为UIWindowScene