// 導(dǎo)航欄高度
#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;\
})
// 狀態(tài)欄高度
#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;\
})
// 標(biāo)簽欄高度
#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;\
})
// 安全區(qū)域底部高度
#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宏定義了不同機(jī)型下導(dǎo)航欄、狀態(tài)欄杭跪、標(biāo)簽欄和安全區(qū)域底部的高度仙逻。如果當(dāng)前設(shè)備運(yùn)行的是 iOS 11 及以上系統(tǒng),則使用[UIApplication sharedApplication].delegate.window.safeAreaInsets來獲取安全區(qū)域的內(nèi)邊距(UIEdgeInsets類型)涧尿,然后計(jì)算出各個(gè)高度系奉。否則,使用默認(rèn)的高度姑廉。
這些宏定義可以涵蓋到各種不同機(jī)型缺亮,包括 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等。