在Xcode11
中新建项目,发现从iOS13
开始,AppDelegate
中再也不管理Window
,而是将功能迁移到了SceneDelegate
中。ios
首先看一下info.plist
的变化 markdown
enable Multipe Windows
--- 是否容许分屏Scene Configuratiton
--- 屏幕配置项Application Session Role
--- 程序屏幕配置规则(为每一个Scene
指定规则)Configuration Name
--- 配置名称Delegate Class Name
--- 代理类名称Storyboard Name
---Storyboard
名称
Default Configuratiton
的默认配置,代理类名称为SceneDelegate
,入口名为Main
的Storyboard
,AppDelegate
中代码以下#pragma mark - UISceneSession lifecycle - (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options { // Called when a new scene session is being created. // Use this method to select a configuration to create the new scene with. return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role]; } 复制代码
Window
,不经过Storyboard
建立,该如何操做?iOS13
及以上系统SceneDelegate
,须要将info.plist
中的Storyboard Name
选项删除(即不指定Storyboard
)SceneDelegate
中代码修改以下:- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions { if (@available(ios 13, *)) { if (scene) { self.window = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene]; self.window.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc]init]]; self.window.rootViewController = nav; [self.window makeKeyAndVisible]; } } } 复制代码
iOS13
的系统info.plist
文件中的Application Scene Manifest
选项SceneDelegate
文件AppDelegate
中的SceneDelegate
方法AppDelegate.h
中添加@property (strong, nonatomic) UIWindow *window; 复制代码
AppDelegate
修改代码以下:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; ViewController * vc = [[ViewController alloc]init]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc]; self.window.rootViewController = nav; [self.window makeKeyAndVisible]; return YES; } 复制代码