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)
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.
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.
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.
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.
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
).
Also if the advice above didn't work, make sure that you have the correct pointer to SceneDelegate
in your Info.plist
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With