iOS - Xcode工程可以运行成功,但是一直处于黑屏状态

·引起黑屏现象的原因分析:  

原因: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的配置项

·结果:项目运行成功,页面展示正常

​​​​​​​

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值