SwiftUI only showing a black screen
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
).
SwiftUI iPad app shows black screen on enabling multiple window support
The window must be initialized as
let window = UIWindow(windowScene: scene as! UIWindowScene)
Black screen on launch when migrating to SwiftUI application flow
The best way to get around this seems to be to leave the info.plist configuration that tells it to look for the SceneDelegate class (UISceneDelegateClassName
), but delete the scene delegate. This will cause warnings in the console on launch, but the black screen issue will stop happening.
iOS : Black screen appears when run my app
Step 1:-
you need to remove the dependency of scene delegate from info.plist
(follow Step show in above Ans of Dipak Ramoliya)
Step 2:-
comment code in scene delegate file
step 3:-
comment code related to scene delegate in App delegate file (connectingSceneSession method and sceneSessions method)
step 4:- Add window variable in AppDelegate file (var window: UIWindow?)
step 5:- create rootViewController in didFinishLaunchingWithOptions method in App Delegate
ex:-
let appDelegate = UIApplication.shared.delegate as! AppDelegate
var rootVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "testController") as! TestController
appDelegate.window?.rootViewController = testController
Launch iOS simulator from Xcode and getting a black screen, followed by Xcode hanging and unable to stop tasks
Surprisingly, what worked for me was going to iOS Simulator menu, and pressing "Reset Content and Settings". (in iOS 13, its under Hardware)
Bad: app building starts with black screen
It may come from your AppDelegate. Could you show us you AppDelegate ?
Apple introduce SceneDelegate that can impact your AppDelegate. If you don't want to use SceneDelegate, you should define the UIWindow in the AppDelegate :
var window: UIWindow?
and init your variable :
window = UIWindow(frame: UIScreen.main.bounds)
in
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
I wish it can help you.
Related Topics
Add Instagram to Uiactivityviewcontroller
What's the Equivalent of Finally in Swift
Swift 3: Atomic_Compare_Exchange_Strong
How to Import a Swift Function Declared in a Compiled .Swiftmodule into Another Swift File
Are the #If Debug Statements Really Needed for Previews in Swiftui to Remove It in a Release Build
Adding a Case to an Existing Enum with a Protocol
Get Current Time as String Swift 3.0
Swiftui Exporting or Sharing Files
Exporting Mp4 Through Avassetexportsession Fails
Swift Protocol and Return Types on Global Functions
Is There a Difference Between "Is" and Iskindofclass()
How to Use Raycast Methods in Realitykit
Convert String to Bool in Swift - via API or Most Swift-Like Approach
How to Detect When Url of Amp Page Changed with Wkwebview
How to Use Alamofire with Custom Headers for Post Request