Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Beta 3 Black Screen

I just converted to beta 3 and my previously working SwiftUI code is now rendering a plain black screen. Was there a change in beta 3 that is causing this. Is there a solution to fix it?

Scene delegate code:

    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()

    }
like image 778
David L Avatar asked Jul 03 '19 02:07

David L


1 Answers

Beta 3 Working Version Of Scene Delgate:

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()
    }
}

Credit to Reddit post for answer.


To clarify, beta 1 used UIWindow(frame: ...) which has now changed to UIWindow(windowScene: ...). The parameter passed is now the current scene and type cast to UIWindowScene.

like image 111
David L Avatar answered Nov 19 '22 16:11

David L