Xcode11-建立項目UISceneDelegate

UISceneDelegate

  • Xcode11中新建項目,發現從iOS13開始,AppDelegate中再也不管理Window,而是將功能遷移到了SceneDelegate中。ios

  • 首先看一下info.plist 的變化 markdown

    info.plist

  • enable Multipe Windows --- 是否容許分屏
  • Scene Configuratiton --- 屏幕配置項
  • Application Session Role --- 程序屏幕配置規則(爲每一個Scene指定規則)
  • Configuration Name --- 配置名稱
  • Delegate Class Name --- 代理類名稱
  • Storyboard Name --- Storyboard名稱
  • 根據上面配置,咱們能夠解讀爲,建立項目時,系統默認爲咱們作了設置,一個名爲Default Configuratiton 的默認配置,代理類名稱爲SceneDelegate,入口名爲MainStoryboard
  • 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;
}
複製代碼
相關文章
相關標籤/搜索