Xcode11 中,如何自定义设置 UIWindow 的 根控制

2019-12-19  本文已影响0人  闻人歌

Xcode11 之前:
window 在 AppDelegate 中设置:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = [SSHomeVC new];
    [self.window makeKeyAndVisible]; 
    return YES;
} 

使用 Xcode11 创建的项目中:
除了自动创建AppDelegate 文件外,还创建了SceneDelegate文件,这适用于 iOS13 之后.此时 AppDelegate 文件中已经没有 UIWindow 对象,而在 SceneDelegate中:

#import <UIKit/UIKit.h>

@interface SceneDelegate : UIResponder <UIWindowSceneDelegate>

@property (strong, nonatomic) UIWindow * window;

@end

此时自定义UIWindow,设置控制器需要:

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    UIWindowScene *windowScene = (UIWindowScene *)scene;
    self.window = [[UIWindow alloc] initWithWindowScene:windowScene];
    self.window.rootViewController = [SSHomeVC new];
    [self.window makeKeyAndVisible];
}
上一篇 下一篇

猜你喜欢

热点阅读