基于Xcode11创建自定义UIWindow手记

笔者最近更新Xcode 11.4,在创建项目之后发现多了苹果分屏技术,新增了SceneDelegate这个文件,另外AppDelegate文件结构也发生了变化,给人一种似曾相识又不同的感觉,总的来说之前熟悉的Window不再由AppDelegate管理,而是交给了SceneDelegate。

如下图即可看出目录结构和info配置变化:

简要介绍Application Scene Manifest分屏配置:

enable Multipe Windows --- 是否允许分屏
Scene Configuratiton --- 屏幕配置项
Application Session Role --- 程序屏幕配置规则(为每个Scene指定规则)
Configuration Name --- 配置名称
Delegate Class Name --- 代理类名称
Storyboard Name --- Storyboard名称

解读如下:

创建项目工程时,系统默认为我们创建了一个名为Default Configuratiton 的默认配置,代理类名称为SceneDelegate,入口名为MainStoryboard,代码如下:

- (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];
}

回到主题,针对这种情况,如果创建我们熟悉的自定义Window呢?

一、针对iOS13系统及以上:保留SceneDelegate,需要修改SceneDelegate里面的代码即可;

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    
    
    if (@available(ios 13, *)) {
        if (scene) {
            self.window = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene];
            self.window.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc]init]];
            self.window.rootViewController = nav;
            [self.window makeKeyAndVisible];
        }
    }
}

效果图:

 

二、针对iOS13系统以下:

a. 删除info.plist文件中的Application Scene Manifest选项;

b. 删除SceneDelegate文件;

c. 删除AppDelegate里面的UISceneSession lifecycle方法;

d. AppDelegate头文件添加window属性;

@property (strong, nonatomic) UIWindow *window;

e. 修改AppDelegate启动方法:


- (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];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    return YES;
}

效果图:

至此,我们又回到了曾经熟悉的开发场景。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

群鸿

感谢认可,多谢打赏。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值