iOS 获取安全边距

352 阅读1分钟
// 导航栏高度
#define kNavBarHeight \
({\
    CGFloat height = 0.0;\
    if (@available(iOS 11.0, *)) {\
        UIEdgeInsets insets = [UIApplication sharedApplication].delegate.window.safeAreaInsets;\
        height = insets.top > 0 ? 44.0 + insets.top : 44.0;\
    } else {\
        height = 64.0;\
    }\
    height;\
})

// 状态栏高度
#define kStatusBarHeight \
({\
    CGFloat height = 0.0;\
    if (@available(iOS 11.0, *)) {\
        UIEdgeInsets insets = [UIApplication sharedApplication].delegate.window.safeAreaInsets;\
        height = insets.top;\
    } else {\
        height = 20.0;\
    }\
    height;\
})

// 标签栏高度
#define kTabBarHeight \
({\
    CGFloat height = 0.0;\
    if (@available(iOS 11.0, *)) {\
        UIEdgeInsets insets = [UIApplication sharedApplication].delegate.window.safeAreaInsets;\
        height = insets.bottom > 0 ? 49.0 + insets.bottom : 49.0;\
    } else {\
        height = 49.0;\
    }\
    height;\
})

// 安全区域底部高度
#define kSafeAreaBottomHeight \
({\
    CGFloat height = 0.0;\
    if (@available(iOS 11.0, *)) {\
        UIEdgeInsets insets = [UIApplication sharedApplication].delegate.window.safeAreaInsets;\
        height = insets.bottom;\
    }\
    height;\
})