Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 420
George Avatar asked Jul 25 '19 15:07

George


People also ask

How do I tell SwiftUI that a certain screen should only launch first?

A lot of people recently asked us how to tell SwiftUI that a certain screen, for instance an onboarding view, should only be shown when the app launches the first time. To implement this functionality, follow the steps below Step 1: Make sure you already set up your onboarding view that should be shown when the app launches for the first time.

How to set the background in SwiftUI?

The proper way to set the screen's background in SwiftUI is by creating a dedicated background view and put it behind every other view. This way, you don't have to change your content's layout. We separate our concerns. You can have a simple or complex background as you want.

How to align one view over another in SwiftUI?

When talking about aligning one view over another, that is the job for ZStack. To add a screen background view by putting it at the bottom of the ZStack. Text("Hello, SwiftUI!") <1> Use ZStack so we can place a background view under the content view. <2> Use color view as background.

How do I add a swift logo to Xcode 12 launch screen?

Let’s say we have a Swift logo in the Assets catalog that is an SVG file. In Xcode 12, SVG images are fully supported (finally). The name for this image is swift: Now we can add the image in the Info.plist in the Launch Screen dictionary using the Image Name key.


2 Answers

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

like image 95
RPatel99 Avatar answered Nov 15 '22 21:11

RPatel99


Also if the advice above didn't work, make sure that you have the correct pointer to SceneDelegate in your Info.plist.

Info.plist

like image 20
Anton Kondrashov Avatar answered Nov 15 '22 20:11

Anton Kondrashov