为什么手动设置根视图控制器显示黑屏?
Why is manually setup root view controller showing black screen?
我已经使用 Xcode 11,Beta 5 为 iOS 13 手动设置了一个根视图控制器。删除了部署信息中对 main 的引用,包括在 info.plist 中删除对 main 的引用在 iOS 之前,我从未发现自己必须这样做 13. window 的设置是在 SceneDelegate 中完成的,嵌套在 willConnectTo 函数中。通常,如果我错过了一步,应用程序就会崩溃。现在我得到一个空白的黑屏,而不是看到我的视图控制器的设置,比如红色背景。所有这些都用于在 beta 5 之前工作。
已执行擦除模拟器上的所有内容和设置。清除构建文件夹并在物理设备上安装 运行 应用程序。还使用了另一台装有 Xcode 11, beta 5 的计算机。所有结果都显示为相同的空白黑屏。我错过了什么?
这是我在 willConnectTo 函数中嵌套的 SceneDelegate 文件中对根视图控制器的手动设置:
let viewCon = ViewController()
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = viewCon
window?.makeKeyAndVisible()
为了确保您在 iOS13 中看到您的根视图控制器,当一切都以编程方式完成时,您必须执行以下操作:
在场景委托中,您必须创建 window 实例和根视图控制器:
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 必须有 "Application Scene Manifest" 条目。它下面应该是 "Enable Multiple Windows" 条目。根据您的应用设置为 YES 或 NO。或者,您还应该有 "Scene Configuration" 条目。
当您检查目标的“常规”选项卡上的 "Supports multiple windows" 设置时,Xcode 添加了所有这些条目。这会将 "Enable Multiple Windows" 条目默认为 YES,因此如果您想要场景而不是多个 windows.
,您可以将其更改为 NO
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
let window = UIWindow(windowScene: windowScene)
self.window = window
let mainstoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewcontroller:UIViewController = mainstoryboard.instantiateViewController(withIdentifier: "YourVCName") as! YourVCName
navigationController = UINavigationController(rootViewController: newViewcontroller)
window.rootViewController = navigationController
window.makeKeyAndVisible()
}
试试这个代码!!
如果您正在使用 ios 13 并且您的 xcode 已更新,那么您应该在场景委托而不是应用程序委托中设置根视图控制器。
当时我遇到了同样的问题,我知道有人已经解决了这个问题,但是,我只是想分享我的方法。
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyBoard.instantiateViewController(withIdentifier: "HomeScreen") as? HomeScreen
self.window?.rootViewController = initialViewController
我改用了这个简单的代码,它的效果非常好:)
我已经使用 Xcode 11,Beta 5 为 iOS 13 手动设置了一个根视图控制器。删除了部署信息中对 main 的引用,包括在 info.plist 中删除对 main 的引用在 iOS 之前,我从未发现自己必须这样做 13. window 的设置是在 SceneDelegate 中完成的,嵌套在 willConnectTo 函数中。通常,如果我错过了一步,应用程序就会崩溃。现在我得到一个空白的黑屏,而不是看到我的视图控制器的设置,比如红色背景。所有这些都用于在 beta 5 之前工作。
已执行擦除模拟器上的所有内容和设置。清除构建文件夹并在物理设备上安装 运行 应用程序。还使用了另一台装有 Xcode 11, beta 5 的计算机。所有结果都显示为相同的空白黑屏。我错过了什么?
这是我在 willConnectTo 函数中嵌套的 SceneDelegate 文件中对根视图控制器的手动设置:
let viewCon = ViewController()
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = viewCon
window?.makeKeyAndVisible()
为了确保您在 iOS13 中看到您的根视图控制器,当一切都以编程方式完成时,您必须执行以下操作:
在场景委托中,您必须创建 window 实例和根视图控制器:
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 必须有 "Application Scene Manifest" 条目。它下面应该是 "Enable Multiple Windows" 条目。根据您的应用设置为 YES 或 NO。或者,您还应该有 "Scene Configuration" 条目。
当您检查目标的“常规”选项卡上的 "Supports multiple windows" 设置时,Xcode 添加了所有这些条目。这会将 "Enable Multiple Windows" 条目默认为 YES,因此如果您想要场景而不是多个 windows.
,您可以将其更改为 NOfunc scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
let window = UIWindow(windowScene: windowScene)
self.window = window
let mainstoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewcontroller:UIViewController = mainstoryboard.instantiateViewController(withIdentifier: "YourVCName") as! YourVCName
navigationController = UINavigationController(rootViewController: newViewcontroller)
window.rootViewController = navigationController
window.makeKeyAndVisible()
}
试试这个代码!! 如果您正在使用 ios 13 并且您的 xcode 已更新,那么您应该在场景委托而不是应用程序委托中设置根视图控制器。
当时我遇到了同样的问题,我知道有人已经解决了这个问题,但是,我只是想分享我的方法。
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyBoard.instantiateViewController(withIdentifier: "HomeScreen") as? HomeScreen
self.window?.rootViewController = initialViewController
我改用了这个简单的代码,它的效果非常好:)