iOS Objective-C,獲取 導(dǎo)航欄高度、狀態(tài)欄高度、標(biāo)簽欄高度、安全區(qū)域高度

// 導(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類型),然后計算出各個高度。否則,使用默認(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等。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容