SwiftUI only showing a black screen

為{幸葍}努か 提交于 2019-12-11 01:49:20

问题


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)


回答1:


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




回答2:


Try re-installing Xcode and see this link to migrate from Xcode beta 3 to Xcode beta 4




回答3:


I had this problem too after going from beta 3 to 4. I created a new project in beta 4 and copied everything over from the old project and it worked.



来源:https://stackoverflow.com/questions/57205114/swiftui-only-showing-a-black-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!