美文网首页
iOS 状态栏,导航栏安全适配

iOS 状态栏,导航栏安全适配

作者: 最强的小强 | 来源:发表于2023-02-23 11:24 被阅读0次

    适配原因:由于新出现的刘海屏,导致状态栏高度和底部安全区域的高度变得不确定了; 在 iOS 13.0 之后,UIApplication 中的 statusBarFrame 属性被废弃⚠️了。官方提供了 UIStatusBarManager 类中的 statusBarFrame 属性来进行获取,具体代码如下:

    #pragma mark- 顶部状态栏+导航栏
    - (CGFloat)setupSafeTopHeight {
        CGFloat navHeight = 44;  // 导航栏固定高度
        CGFloat statusHeight = 20; // 状态栏高度
        if (@available(iOS 13.0, *)) {
            NSSet *set = [UIApplication sharedApplication].connectedScenes;
            UIWindowScene *scene = [set anyObject];
            UIStatusBarManager *statusBarManager = scene.statusBarManager;
            statusHeight = statusBarManager.statusBarFrame.size.height;
        } else {
            statusHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
        }
        return navHeight + statusHeight;
    }
    
    #pragma mark- 底部导航栏+底部安全区域
    - (CGFloat)setupSafeBottomHeight {
        CGFloat height = 49; // 底部导航栏固定高度
        if (@available(iOS 13.0, *)) {
            NSSet *set = [UIApplication sharedApplication].connectedScenes;
            UIWindowScene *scene = [set anyObject];
            UIWindow *window = scene.windows.firstObject;
            height = height + window.safeAreaInsets.bottom;
        } else if (@available(iOS 11.0, *)) {
            UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
            height = height + window.safeAreaInsets.bottom;
        }
        return height;
    }
    

    相关文章

      网友评论

          本文标题:iOS 状态栏,导航栏安全适配

          本文链接:https://www.haomeiwen.com/subject/duwzkdtx.html