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

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];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。