为什么手动设置的根视图控制器显示黑屏? [英] Why is manually setup root view controller showing black screen?

查看:47
本文介绍了为什么手动设置的根视图控制器显示黑屏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

已执行擦除模拟器上的所有内容和设置.清除了构建文件夹,并已在物理设备上运行了该应用程序.还使用了另一台Xcode 11(测试版5)的计算机.所有结果都显示在相同的黑屏上.我想念什么?

这是我在willConnectTo函数中嵌套的SceneDelegate文件中的根视图控制器的手动设置:

 让viewCon = ViewController()窗口= UIWindow(框架:UIScreen.main.bounds)窗口?.rootViewController = viewCon窗口?.makeKeyAndVisible() 

解决方案

为确保在以编程方式完成所有操作后,您可以在iOS 13中看到您的根视图控制器,您必须执行以下操作:

在场景委托中,您必须创建窗口实例和根视图控制器:

  Class SceneDelegate类:UIResponder,UIWindowSceneDelegate {var window:UIWindow?func scene(_ scene:UIScene,willConnectTo会话:UISceneSession,选项connectionOptions:UIScene.ConnectionOptions){警卫让winScene =(场景为UIWindowScene)否则{return}//根据需要创建根视图控制器让vc = ViewController()让nc = UINavigationController(rootViewController:vc)//创建窗口.确保使用此初始值设定项而不是框架一.让win = UIWindow(windowScene:winScene)win.rootViewController = ncwin.makeKeyAndVisible()窗口=赢}} 

您的Info.plist必须具有应用程序场景清单"条目.在它下面应该是启用多个Windows"条目.根据您的应用设置为是或否.(可选)您还应该具有场景配置"条目.

当您在目标的常规"选项卡上选中支持多个窗口"设置时,所有这些条目均由Xcode添加.默认情况下,启用多个窗口"条目将为是",因此,如果您需要场景而不是多个窗口,则可以将其更改为否".

I have manually setup a root view controller for iOS 13 using Xcode 11, Beta 5. Deleted references to main in the deployment info including removing reference to main in info.plist which I never found myself having to do prior to iOS 13. Setup for window is done in SceneDelegate, nested in willConnectTo function. Normally the app would crash if I missed a step. Now I'm getting a blank black screen instead of seeing what my view controller is setup for, say a red background. All of this use to work prior to beta 5.

Have performed erase all content and settings on the simulator. Cleared the build folder and have ran the app on a physical device. Also have used another computer with Xcode 11, beta 5. All results to the same blank black screen. What am I missing?

Here is my manual setup for root view controller in SceneDelegate file nested in willConnectTo function:

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

解决方案

To ensure you see your root view controller in iOS 13 when everything is done programmatically, you must do the following:

In the scene delegate, you must create the window instance and the 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
    }
}

Your Info.plist has to have the "Application Scene Manifest" entry. Below it should be the "Enable Multiple Windows" entry. Set to YES or NO as appropriate to your app. Optionally you should also have the "Scene Configuration" entry.

All of these entries are added by Xcode when you check the "Supports multiple windows" setting on the General tab of your target. This will default the "Enable Multiple Windows" entry to YES so you can change that to NO if you want scenes but not multiple windows.

这篇关于为什么手动设置的根视图控制器显示黑屏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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