Xcode11 對(duì)應(yīng)的iOS系統(tǒng)為 iOS13。
Xcode11 新建項(xiàng)目時(shí)會(huì)多出一個(gè)SceneDelegate類(lèi),這個(gè)類(lèi)里面的代碼只有 iOS13系統(tǒng)的手機(jī)才會(huì)執(zhí)行。
當(dāng)啟動(dòng)方式采用手動(dòng)創(chuàng)建window設(shè)置rootViewController時(shí),Xcode11的window初始化方式與以前有所不同。
方式一
Xcode11 需要在 SceneDelegate類(lèi)里面給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;
}
??注意:在運(yùn)行完成后,頁(yè)面是全黑,因?yàn)闆](méi)有給ViewController這個(gè)頁(yè)面設(shè)置背景色導(dǎo)致的,不是代碼的問(wèn)題。
方式二
參照以往的AppDelegate里面
刪除SceneDelegate代理文件 (可選)
刪除 Info.plist里面的Application Scene Manifest配置(一定要?jiǎng)h除)
刪除 AppDelegate代理的兩個(gè)方法:
application:configurationForConnectingSceneSession:options:
application: didDiscardSceneSessions:
這兩個(gè)方法一定要?jiǎng)h除,否則使用純代碼創(chuàng)建的Window和導(dǎo)航控制器UINavigationController不會(huì)生效。