Swift - 更改启动方式(设置根视图)

App的启动方式有两种:Storyboard启动,AppDelegate启动。

iOS13以后,新增了SceneDelegate类,处理 App 生命周期和新的 Scene Session 生命周期,在AppDelegate中没有了window属性,而是在SceneDelegate中。SceneDelegate负责原有AppDelegate的某些功能, window的概念由窗口变为场景

更改为代码启动

改为SceneDelegate启动

  1. 删除Main.storyboard中的代表根视图的启动箭头
  2. 在SceneDelegate的func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)中添加根视图
window = UIWindow(windowScene: scene as! UIWindowScene)        
let rootVC = TabbarVC()
window?.rootViewController = rootVC
window?.makeKeyAndVisible()

改为AppDelegate启动。

用xcode11之后版本新建的项目,默认是配置了SceneDelegate的。适配iOS13之前的代码,可以将其改为从Appdelegate启动。

  1. 删除Main.storyboard中的代表根视图的启动箭头
  2. 删除SceneDelegate文件
  3. 删除info中的启动配置
  4. 删除Appdelegate中关于SceneDelegate的代码
  5. 在Appdelegate中定义window
let window = UIWindow()
  1. 在Appdelegate的func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool中设置根视图
        let vc = ViewController()
        let navi = UINavigationController.init(rootViewController: vc)
        self.window.rootViewController = navi
        self.window.makeKeyAndVisible()
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • iOS13内容全介绍 ​ WWDC2019发布了iOS13,将随新iPhone发售正式发布。对于移动开发而言,最关...
    sycasl阅读 5,751评论 0 0
  • iOS13 项目中的SceneDelegate类有什么作用?自从Xcode11发布以来,当你使用新XCode创建一...
    乐Coding阅读 30,641评论 14 61
  • iOS13 项目中的SceneDelegate类有什么作用?自从Xcode11发布以来,当你使用新XCode创建一...
    代码移动工程师阅读 11,278评论 3 36
  • 前言: iOS13的API的变动和适配问题,我从新特性适配、API 适配、方法弃用、工程适配、SDK 适配、其他问...
    smile丽语阅读 1,307评论 0 4
  • 为了实现iPadOS支持多窗口,Xcode11后创建新工程默认会通过 UIScene 创建并管理多个 UIWind...
    codeTao阅读 6,440评论 1 15