首页 > 解决方案 > 从 iOS 13 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)

    // Override point for customization after application launch.
    let context = AppDelegate.shared.persistentContainer.viewContext
    context.automaticallyMergesChangesFromParent = true

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

标签: swiftios13

解决方案


这是最初的默认值。从 beta 3 开始,windows 需要像这样初始化:

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

推荐阅读