UISceneDelegate
在
Xcode11
中新建項(xiàng)目,發(fā)現(xiàn)從iOS13
開(kāi)始,AppDelegate
中不再管理Window
挎袜,而是將功能遷移到了SceneDelegate
中窘游。-
首先看一下
info.plist
的變化
enable Multipe Windows
--- 是否允許分屏Scene Configuratiton
--- 屏幕配置項(xiàng)Application Session Role
--- 程序屏幕配置規(guī)則(為每個(gè)Scene
指定規(guī)則)Configuration Name
--- 配置名稱Delegate Class Name
--- 代理類名稱Storyboard Name
---Storyboard
名稱
- 根據(jù)上面配置,我們可以解讀為惜纸,創(chuàng)建項(xiàng)目時(shí)叶撒,系統(tǒng)默認(rèn)為我們做了設(shè)置,一個(gè)名為
Default Configuratiton
的默認(rèn)配置堪簿,代理類名稱為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];
}
那么問(wèn)題來(lái)了,想要自定義Window
,不通過(guò)Storyboard
創(chuàng)建椭更,該如何操作?
在iOS13
及以上系統(tǒng)
- 如果繼續(xù)使用
SceneDelegate
哪审,需要將info.plist
中的Storyboard Name
選項(xiàng)刪除(即不指定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
的系統(tǒng)
- 刪除
info.plist
文件中的Application Scene Manifest
選項(xiàng) - 刪除
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;
}