SwiftUI仅显示黑屏 [英] SwiftUI only showing a black screen

查看:106
本文介绍了SwiftUI仅显示黑屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大约两周前去度假之前,我已经在SwiftUI中创建了几个很好的项目.当我回来时,我更新了Xcode和我的iPhone.

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.

SwiftUI的代码无关紧要,因为它之前都工作得很好.在多个经过测试的项目中,我得到的只是一个纯黑的屏幕.

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.

什么原因导致我的所有项目在更新Xcode和我的设备之前工作时仅显示黑屏?

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

版本:

我的设备-13.0 beta 4

My device - 13.0 beta 4

模拟器不起作用-不确定版本

Simulators don't work - not sure on versions

Xcode-beta-11.0 beta 4(11M374r)

Xcode-beta - 11.0 beta 4 (11M374r)

推荐答案

SceneDelegate.swift中替换

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

使用

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

如果最初在ContentView()上附加了.environtmentObject,请不要忘记在上面的代码中将其添加到ContentView()上.

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

在Xcode beta 4中创建新项目时,我发布的第二个代码块是在SceneDelegate.swift中自动生成的代码.我发布的第一个代码块是beta 4之前所有版本中自动生成的代码.正如您在第二个块中看到的那样,该窗口现在使用SceneDelegate函数scene(scene:, session:, connectionOptions:)提供的scene初始化,而不是一个CGRect(UIScreen.main.bounds).

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仅显示黑屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆