·引起黑屏现象的原因分析:
原因:iOS工程代码中缺少window属性
详细说明:Xcode11以后,Xcode会默认使用Scene进行多窗口分屏管理,我们可以在SceneDelegate文件中看到,window属性已经被默认创建,如下图所示:
当我们没有多窗口分屏的需求时,可以删除工程中Scene的相关代码(SceneDelegate文件、AppDelegate中有关Scene的方法、Info.plist文件中的Scene配置项,这里可参照下文的步骤二至四进行操作)。
删除Scene代码后,APP生命周期管理由 AppDelegate完全负责,代码运行成功时,模拟器黑屏,控制台打印如下错误信息:
从此处可以看出,模拟器黑屏是由于缺少window属性引起的。这是由于APP的所有view控件都是在window窗口属性上展示的,AppDelegate文件没有默认创建window属性,所以是无法显示出任何view控件的。
·解决方案如下:
步骤一:在AppDelegate文件中添加window属性
步骤二:删除SceneDelegate文件
步骤三:删除AppDelegate中,有关Scene的两个方法,如下所示:
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
步骤四:删除Info文件中,Scene的配置项
·结果:项目运行成功,页面展示正常