iOS适配灵动岛safeArea顶部高度变化

灵动岛出来后,发现线上iOS应用顶部出现白条现象

在这里插入图片描述

我们的应用是嵌套wkwebview做的,之前修改顶部状态栏颜色的代码是这样的

- (void) changeStatusBarColor: (NSString *) hexColor {
    if (@available(iOS 13.0, *)) {
        // iOS 13  弃用keyWindow属性  从所有windowl数组中取
        UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame] ;
        statusBar.backgroundColor = [self colorWithHexString:hexColor];
        [[UIApplication sharedApplication].keyWindow addSubview:statusBar];
    }else{
        UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
        if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
            statusBar.backgroundColor = [self colorWithHexString:hexColor];
        }
    }
}

查了一下发现,是灵动岛的机型系统的statusbar高度和safeArea顶部高度不一致导致的,刘海机型是statusbar和safeArea一样是47,灵动岛机型statusbar高度是54,safeArea顶部高度是59,导致出现高度5的白边

所以适配方案为,修改statusbar高度和safeArea顶部高度一致即可,代码如下

- (void) changeStatusBarColor: (NSString *) hexColor {
    if (@available(iOS 13.0, *)) {
        // iOS 13  弃用keyWindow属性  从所有windowl数组中取
        UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame] ;
        statusBar.backgroundColor = [self colorWithHexString:hexColor];
        // 修改statusBar高度适配灵动岛
        [statusBar setFrame:CGRectMake(statusBar.frame.origin.x, statusBar.frame.origin.y, statusBar.frame.size.width, [UIApplication sharedApplication].keyWindow.safeAreaInsets.top)];
        [[UIApplication sharedApplication].keyWindow addSubview:statusBar];
    }else{
        UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
        if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
            statusBar.backgroundColor = [self colorWithHexString:hexColor];
        }
    }
}

效果达成

在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值