11

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

    }
David L
  • 4,347
  • 3
  • 18
  • 30

2 Answers2

24

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.

Leon Storey
  • 3,274
  • 2
  • 25
  • 40
David L
  • 4,347
  • 3
  • 18
  • 30
  • I created my current project in beta 2 and I got the first scene delegate you have posted, which beta was you on when you created this project? – Sudara Jul 03 '19 at 04:11
  • @Sudara I think my original code was from beta 1. I think this was related to the beta 3 changes from yesterday. – David L Jul 03 '19 at 11:13
  • mmm...what is the difference? I've the same problem, but I haven't solved it – nunzio giulio caggegi Jul 05 '19 at 14:59
  • @nunzio Look at the updated code carefully. It has a `if let windowScene = scene as? UIWindowScene` before the original code block. – David L Jul 05 '19 at 20:01
  • @DavidL sorry for my answer, it's not evident. even so it doesn't work for me – nunzio giulio caggegi Jul 06 '19 at 08:07
  • @nunziogiuliocaggegi The change is in `UIWindow(windowScene: windowScene)`, in beta 1 this used to be `UIWindow(frame: ...)`. The condition is for type casting `windowScene` to be used within the updated `UIWindow` init. – Leon Storey Jul 14 '19 at 14:01
  • It worked for... i was banging my head!! Thanks a lot – anoop4real Dec 03 '19 at 11:06
0

I have no storyboard in my project, All UI is coded programmatically (Not swift UI).

On the launching of the application, after the splash screen, the only Black screen was appearing.

The solution worked for me is, I have disabled/unchecked the "Supports multiple windows" from general settings.

enter image description here

indrajit
  • 303
  • 1
  • 14