iOS 在AppDelegate中添加UIWindow
2021-05-14 本文已影响0人
liang1030
iOS13以后AppDelegate.h文件中没有了window属性,而是在SceneDelegate.h中,
iOS13中appdelegate的职责发现了改变:
iOS13之前,Appdelegate的职责全权处理App生命周期和UI生命周期;
iOS13之后,Appdelegate的职责是:
1、处理 App 生命周期
2、新的 Scene Session 生命周期
iOS13之后,Appdelegate不在负责UI生命周期,所有UI生命周期交给SceneDelegate处理。
在AppDelegate中添加window的方法:
1,在AppDelegate.h中添加
@property (nonatomic, strong) UIWindow *window;
2,在AppDelegate.m中添加
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
ViewController *vc = [[ViewController alloc] init];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
return YES;
}
3,删除AppDelegate.m中以下代码
#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];
}
- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
4,删除Info.plist中以下参数 (Main storyboard file base name, Application Scene Manifest)
<key>UISceneConfigurations</key>
<dict>
<key>UIWindowSceneSessionRoleApplication</key>
<array>
<dict>
<key>UISceneConfigurationName</key>
<string>Default Configuration</string>
<key>UISceneDelegateClassName</key>
<string>SceneDelegate</string>
<key>UISceneStoryboardFile</key>
<string>Main</string>
</dict>
</array>
</dict>
<key>UIMainStoryboardFile</key>
<string>Main</string>
注意:window需要设置根视图,
ViewController上的view需要设置背景色,不然是黑色的。