刘海屏手机在部分页面通过[[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom得到底部安全区高度为0问题

刘海屏手机在部分页面通过[[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom得到底部安全区高度为0问题,如何解决?
如我们在播放器SelVideoPlayer中获取到刘海屏手机底部安全区高度为0,我顶部安全区也是0。正如这样下面的定义:

#define kMainTopHeight  ({ float space = 64.0; if (@available(iOS 11.0, *)) space = ([[UIApplication sharedApplication] delegate].window.safeAreaInsets.top+44); (space); })
#define kMainBottomHeight ({ float space = 49.0; if (@available(iOS 11.0, *)) space = ([[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom + 49); (space); })

分析原因应该是窗口的问题。那如何解决呢?
可以通过重新获取keyWindow来正确找到底部安全高度和顶部安全区高度。

#define Base_BottomHeight ([FMConfig config].safeBottomHeight)
// 底部安全区域高度
#define BR_BOTTOM_MARGIN \
({CGFloat safeBottomHeight = 0;\
if (@available(iOS 11.0, *)) {\
safeBottomHeight = BRGetKeyWindow().safeAreaInsets.bottom;\
}\
(safeBottomHeight);})


/** 获取 keyWindow */
static inline UIWindow *BRGetKeyWindow(void) {
    UIWindow *keyWindow = nil;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 // 编译时检查SDK版本:Xcode11+编译会调用(iOS SDK 13.0 以后版本的处理)
    if (@available(iOS 13.0, *)) {
        NSSet<UIScene *> *connectedScenes = [UIApplication sharedApplication].connectedScenes;
        for (UIScene *scene in connectedScenes) {
            if (scene.activationState == UISceneActivationStateForegroundActive && [scene isKindOfClass:[UIWindowScene class]]) {
                UIWindowScene *windowScene = (UIWindowScene *)scene;
                for (UIWindow *window in windowScene.windows) {
                    if (window.isKeyWindow) {
                        keyWindow = window;
                        break;
                    }
                }
            }
        }
    } else
#endif
    {
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 130000
        return [UIApplication sharedApplication].keyWindow;
#endif
    }
    
    return keyWindow;
}

该方法绝大部份情况下可以,但是也有部份场景失效。如我在tabbar首页,隐藏状态栏,采用TXVodPlayer插入表格图层的页面,竟然也是算出的iPhone xr底部tabbar高度是49。

- (CGFloat)statusHeight{
    if (_statusHeight == 0) {
        if (@available(iOS 13.0, *)) {
            _statusHeight = self.windowScene.statusBarManager.statusBarFrame.size.height;
        } else {
            _statusHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
        }
    }
    return _statusHeight;
}

- (BOOL)isIPhoneX{
    return self.statusHeight > 20;
}

- (CGFloat)safeBottomHeight{
    return self.isIPhoneX ? 34 : 0;
}

- (CGFloat)tabBarHeight{
    return self.isIPhoneX ? 83 : 49;
}

对于该种场景的解决方法:

#define kMainBottomHeight ({ float space = 49.0; if ([[UIApplication sharedApplication] statusBarFrame].size.height>20) space = 83.0; (space); })

当然这种方法也不是万能的方法,需要结合具体场景来使用。

你可能感兴趣的:(疑难问题,Object,C,UI,ios,objective-c,开发语言)