SwiftUI Beta 3黑屏

时间:2019-07-03 02:55:14

标签: swift swiftui xcode11

我刚刚转换为Beta 3,以前使用的SwiftUI代码现在正在呈现纯黑屏幕。 Beta 3是否发生了变化,从而导致了此问题?有解决方案吗?

场景委托代码:

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

        // Use a UIHostingController as window root view controller


    let window = UIWindow(frame: UIScreen.main.bounds)


     window.rootViewController = UIHostingController(rootView: ContentView())
     self.window = window
     window.makeKeyAndVisible()

    }

2 个答案:

答案 0 :(得分:15)

Scene Delgate的Beta 3工作版本:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

    // Use a UIHostingController as window root view controller
    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(rootView: ContentView())
        self.window = window
        window.makeKeyAndVisible()
    }
}

Credit to Reddit post for answer.


为明确起见,测试版1使用了UIWindow(frame: ...),现在已更改为UIWindow(windowScene: ...)。现在,传递的参数是当前场景,并键入强制转换为UIWindowScene

答案 1 :(得分:0)

我的项目中没有情节提要,所有UI均以编程方式编码(不是快速的UI)。

启动应用程序时,在初始屏幕之后,仅出现了黑屏。

对我有用的解决方案是 我已从常规设置中禁用/取消选中“支持多个窗口”。

enter image description here