Search code examples
iosxcodeios-simulatorswiftui

SwiftUI only showing a black screen


I had created a few projects in SwiftUI that were perfectly fine, before I went on holiday around 2 weeks ago. When I got back, I updated Xcode and my iPhone.

The code for SwiftUI is not relevant as it was all working perfectly fine before. All I am getting on multiple projects tested is just a plain black screen.

What could be causing all my projects just to show a black screen, when they worked before updating Xcode and my device?

Versions:

My device - 13.0 beta 4

Simulators don't work - not sure on versions

Xcode-beta - 11.0 beta 4 (11M374r)


Solution

  • In SceneDelegate.swift replace

    let window = UIWindow(frame: UIScreen.main.bounds)
    window.rootViewController = UIHostingController(rootView: ContentView())
    self.window = window
    window.makeKeyAndVisible()
    

    with

    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(rootView: ContentView())
        self.window = window
        window.makeKeyAndVisible()
    }
    

    If you had a .environtmentObject attached to ContentView() originally, don't forget to add that onto the ContentView() in the above code.

    When you create a new project in Xcode beta 4, the second code block I posted is what is automatically generated in SceneDelegate.swift. The first code block I posted is what was automatically generated in all versions prior to beta 4. As you can see in the second block, the window is now initialized with the scene provided by the SceneDelegate function scene(scene:, session:, connectionOptions:) instead of a CGRect (UIScreen.main.bounds).