在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; } 複製代碼