gpt4 book ai didi

swift - 为什么手动设置 Root View Controller 显示黑屏?

转载 作者:行者123 更新时间:2023-12-04 11:44:43 24 4
gpt4 key购买 nike

我已经使用 Xcode 11,Beta 5 手动设置了 iOS 13 的 Root View Controller 。删除了部署信息中对 main 的引用,包括删除了 info.plist 中对 main 的引用,在 iOS 13 之前我从未发现自己必须这样做。窗口在 SceneDelegate 中完成,嵌套在 willConnectTo 函数中。通常,如果我错过了一个步骤,应用程序会崩溃。现在我得到一个空白的黑屏,而不是看到我的 View Controller 的设置,比如红色背景。所有这些都可以在 beta 5 之前使用。

已执行删除模拟器上的所有内容和设置。清除构建文件夹并在物理设备上运行应用程序。还使用了另一台装有 Xcode 11,beta 5 的计算机。所有结果都显示为相同的空白黑屏。我错过了什么?

这是嵌套在 willConnectTo 函数中的 SceneDelegate 文件中 Root View Controller 的手动设置:

let viewCon = ViewController()
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = viewCon
window?.makeKeyAndVisible()

最佳答案

为确保您在以编程方式完成所有操作后在 iOS 13 中看到您的 Root View Controller ,您必须执行以下操作:

在场景委托(delegate)中,您必须创建窗口实例和 Root View Controller :

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let winScene = (scene as? UIWindowScene) else { return }

// Create the root view controller as needed
let vc = ViewController()
let nc = UINavigationController(rootViewController: vc)

// Create the window. Be sure to use this initializer and not the frame one.
let win = UIWindow(windowScene: winScene)
win.rootViewController = nc
win.makeKeyAndVisible()
window = win
}
}

您的 Info.plist 必须具有“应用程序场景 list ”条目。它下面应该是“启用多个窗口”条目。根据您的应用设置为 YES 或 NO。或者,您还应该有“场景配置”条目。

当您在目标的“常规”选项卡上检查“支持多个窗口”设置时,Xcode 会添加所有这些条目。这会将“启用多个窗口”条目默认为"is",因此如果您想要场景而不是多个窗口,您可以将其更改为“否”。

关于swift - 为什么手动设置 Root View Controller 显示黑屏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57451496/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com