// 导航栏高度
#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;\
})
上述代码中,kNavBarHeight、kStatusBarHeight、kTabBarHeight和kSafeAreaBottomHeight宏定义了不同机型下导航栏、状态栏、标签栏和安全区域底部的高度。如果当前设备运行的是 iOS 11 及以上系统,则使用[UIApplication sharedApplication].delegate.window.safeAreaInsets来获取安全区域的内边距(UIEdgeInsets类型),然后计算出各个高度。否则,使用默认的高度。
这些宏定义可以涵盖到各种不同机型,包括 iPhone X、iPhone XR、iPhone XS、iPhone XS Max、iPhone 11、iPhone 11 Pro、iPhone 11 Pro Max、iPhone 12、iPhone 12 Pro、iPhone 12 mini、iPhone 12 Pro Max、iPhone 13、iPhone 13 mini、iPhone 13 Pro、iPhone 13 Pro Max、iPhone 14、iPhone 14 mini、iPhone 14 Pro、iPhone 14 Pro Max等。