App的启动方式有两种:Storyboard启动,AppDelegate启动。
iOS13以后,新增了SceneDelegate类,处理 App 生命周期和新的 Scene Session 生命周期,在AppDelegate中没有了window属性,而是在SceneDelegate中。SceneDelegate负责原有AppDelegate的某些功能, window的概念由窗口
变为场景
。
更改为代码启动
改为SceneDelegate启动
- 删除Main.storyboard中的代表根视图的启动箭头
- 在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启动。
- 删除Main.storyboard中的代表根视图的启动箭头
- 删除SceneDelegate文件
-
删除info中的启动配置
-
删除Appdelegate中关于SceneDelegate的代码
- 在Appdelegate中定义
window
let window = UIWindow()
- 在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()
网友评论