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

2023-02-23  本文已影响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;
}
上一篇下一篇

猜你喜欢

热点阅读