(Objective-C) SceneDelegate使用及移除注意事项

一) 项目中使用SceneDelegate

1)使用Main.StoryBoard(Default)

无需特殊处理,Xcode 11及其以上版本,默认生成使用'Main.StoryBoard'模板

2)不使用Main.StoryBoard

2.1)在info.plist文件中找到Storyboard Name键值对,将其移除

Storyboard Name

2.2)在Base.lproj文件夹中找到Main.storyboard,将其移除

Main

2.3)在侧边栏TARGETS下,选中General选项卡,找到Deployment Info项,在Status Bar Style选项中,勾选Supports multiple windows选项

Status Bar Style

2.4)在AppDelegate.h文件中,新增window属性

@property (strong, nonatomic) UIWindow *window;

2.5)打开SceneDelegate.m文件,在willConnectToSession代理方法中,创建window窗口,初始化rootViewController

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    UIWindowScene *windowScene = (UIWindowScene *)scene;
    self.window = [[UIWindow alloc] initWithWindowScene:windowScene];
    self.window.frame = windowScene.coordinateSpace.bounds;
    self.window.rootViewController = ViewController.new;
    [self.window makeKeyAndVisible];
}

二) 项目中不使用SceneDelegate

1)使用Main.StoryBoard

1.1)在info.plist文件中找到Application Scene Manifest键值对,将其移除

Application Scene Manifest

1.2)打开AppDelegate.m文件,找到UISceneSession lifecycle代理方法,将其全部移除,
configurationForConnectingSceneSessiondidDiscardSceneSessions两个方法

#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.
}

2)不使用Main.StoryBoard

2.1)在 info.plist 文件中找到 Application Scene Manifest 键值对,将其移除

Application Scene Manifest

2.2)在 info.plist 文件中找到 Main storyboard file base name 键值对,将其移除

Main storyboard file base name

2.3)在Base.lproj文件夹中找到Main.storyboard,将其移除

Main

2.4)在AppDelegate.h文件中,新增window属性

@property (strong, nonatomic) UIWindow *window;

2.5)打开AppDelegate.m文件,在didFinishLaunchingWithOptions代理方法中,创建window窗口,初始化rootViewController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = UIColor.whiteColor;
    self.window.rootViewController = UITabBarController.new;
    [self.window makeKeyAndVisible];
    return true;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容