Xcode11 對應(yīng)的iOS系統(tǒng)為 iOS13敬扛。
Xcode11 新建項目時會多出一個SceneDelegate類,這個類里面的代碼只有 iOS13系統(tǒng)的手機才會執(zhí)行。
當啟動方式采用手動創(chuàng)建window設(shè)置rootViewController時,Xcode11的window初始化方式與以前有所不同受楼。
方式一
Xcode11 需要在 SceneDelegate類里面給window添加rootViewController, SceneDelegate里面window是已經(jīng)創(chuàng)建好了的,不需要再次創(chuàng)建呼寸。
#import "SceneDelegate.h"
#import "ViewController.h"
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
self.window.rootViewController = [[ViewController alloc] init];
[self.window makeKeyAndVisible];
///或者使用下面的代碼
/*
self.window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
self.window.windowScene = (UIWindowScene *)scene;
self.window.rootViewController = [[ViewController alloc] init];
[self.window makeKeyAndVisible];
*/
}
兼容iOS13以下的版本,需要在AppDelegate中創(chuàng)建window設(shè)置rootViewController
#import "AppDelegate.h"
#import "ViewController.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (@available(iOS 13,*)) {
} else {
self.window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
self.window.rootViewController = [[BaseTabBarController alloc] init];
[self.window makeKeyAndVisible];
}
return YES;
}
??注意:在運行完成后猴贰,頁面是全黑对雪,因為沒有給ViewController這個頁面設(shè)置背景色導(dǎo)致的,不是代碼的問題米绕。
方式二
參照以往的AppDelegate里面
刪除SceneDelegate代理文件 (可選)
刪除 Info.plist里面的Application Scene Manifest配置(一定要刪除)
刪除 AppDelegate代理的兩個方法:
application:configurationForConnectingSceneSession:options:
application: didDiscardSceneSessions:
這兩個方法一定要刪除瑟捣,否則使用純代碼創(chuàng)建的Window和導(dǎo)航控制器UINavigationController不會生效馋艺。