一般獲取 「狀態(tài)欄+導(dǎo)航欄高度」的方法
1 采用的是類似于下面的判斷是否為劉海屏,然后返回固定值的寫法缀拭。
// 狀態(tài)欄高度咳短,iPhoneX 是判斷是否為劉海屏
#define StatusBar_Height (iPhoneX ? 44.0f : 20.0f)
早些時(shí)間,這種方法確實(shí)是沒錯(cuò)的蛛淋,但是在 iOS 14 系統(tǒng)之后咙好,劉海屏手機(jī)的狀態(tài)欄高度就不再統(tǒng)一是 44 了。 劉海屏機(jī)型的狀態(tài)欄高度 其中 iPhone 13 系列和 iPhone 12 系列是一致的褐荷,就不再重復(fù)列舉
iPhone XR/11 ------->48
iPhone X/11 Pro/ 11 Pro Max/12 mini -------> 44
iPhone 12/12 Pro/Pro Max -------->47
2 利用系統(tǒng)方法獲取狀態(tài)欄高度
因?yàn)樯厦婺欠N寫死的方法已經(jīng)不能滿足我們的需求了勾效,于是我們采用系統(tǒng)的方法來獲取。在 iOS 13.0 之前叛甫,我們可以通過 UIApplication 單例中的 statusBarFrame 屬性獲取狀態(tài)狀態(tài)欄改度层宫,代碼如
[UIApplication sharedApplication].statusBarFrame.size.height;
但是在 iOS 13.0 之后,UIApplication 單例中的 statusBarFrame 屬性被廢棄??了其监。官方希望能我們使用 UIStatusBarManager 類中的 statusBarFrame 熟悉來進(jìn)行獲取萌腿,代碼如下:
if (@available(iOS 13.0, *)) {
NSSet *set = [UIApplication sharedApplication].connectedScenes;
UIWindowScene *windowScene = [set anyObject];
UIStatusBarManager *statusBarManager = windowScene.statusBarManager;
return statusBarManager.statusBarFrame.size.height;
}
3 代碼模版
在 UIDevice+VGAddition.h 中,對(duì)各方法進(jìn)行了聲明抖苦。
@interface UIDevice (VGAddition)
/// 頂部安全區(qū)高度
+ (CGFloat)vg_safeDistanceTop;
/// 底部安全區(qū)高度
+ (CGFloat)vg_safeDistanceBottom;
/// 頂部狀態(tài)欄高度(包括安全區(qū))
+ (CGFloat)vg_statusBarHeight;
/// 導(dǎo)航欄高度
+ (CGFloat)vg_navigationBarHeight;
/// 狀態(tài)欄+導(dǎo)航欄的高度
+ (CGFloat)vg_navigationFullHeight;
/// 底部導(dǎo)航欄高度
+ (CGFloat)vg_tabBarHeight;
/// 底部導(dǎo)航欄高度(包括安全區(qū))
+ (CGFloat)vg_tabBarFullHeight;
@end
在 UIDevice+VGAddition.m 中毁菱,對(duì)聲明的各方法進(jìn)行了實(shí)現(xiàn)米死。
#import "UIDevice+VGAddition.h"
@implementation UIDevice (VGAddition)
/// 頂部安全區(qū)高度
+ (CGFloat)vg_safeDistanceTop {
if (@available(iOS 13.0, *)) {
NSSet *set = [UIApplication sharedApplication].connectedScenes;
UIWindowScene *windowScene = [set anyObject];
UIWindow *window = windowScene.windows.firstObject;
return window.safeAreaInsets.top;
} else if (@available(iOS 11.0, *)) {
UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
return window.safeAreaInsets.top;
}
return 0;
}
/// 底部安全區(qū)高度
+ (CGFloat)vg_safeDistanceBottom {
if (@available(iOS 13.0, *)) {
NSSet *set = [UIApplication sharedApplication].connectedScenes;
UIWindowScene *windowScene = [set anyObject];
UIWindow *window = windowScene.windows.firstObject;
return window.safeAreaInsets.bottom;
} else if (@available(iOS 11.0, *)) {
UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
return window.safeAreaInsets.bottom;
}
return 0;
}
/// 頂部狀態(tài)欄高度(包括安全區(qū))
+ (CGFloat)vg_statusBarHeight {
if (@available(iOS 13.0, *)) {
NSSet *set = [UIApplication sharedApplication].connectedScenes;
UIWindowScene *windowScene = [set anyObject];
UIStatusBarManager *statusBarManager = windowScene.statusBarManager;
return statusBarManager.statusBarFrame.size.height;
} else {
return [UIApplication sharedApplication].statusBarFrame.size.height;
}
}
/// 導(dǎo)航欄高度
+ (CGFloat)vg_navigationBarHeight {
return 44.0f;
}
/// 狀態(tài)欄+導(dǎo)航欄的高度
+ (CGFloat)vg_navigationFullHeight {
return [UIDevice vg_statusBarHeight] + [UIDevice vg_navigationBarHeight];
}
/// 底部導(dǎo)航欄高度
+ (CGFloat)vg_tabBarHeight {
return 49.0f;
}
/// 底部導(dǎo)航欄高度(包括安全區(qū))
+ (CGFloat)vg_tabBarFullHeight {
return [UIDevice vg_tabBarHeight] + [UIDevice vg_safeDistanceBottom];
}
@end
Swift 版本
extension UIDevice {
/// 頂部安全區(qū)高度
static func vg_safeDistanceTop() -> CGFloat {
if #available(iOS 13.0, *) {
let scene = UIApplication.shared.connectedScenes.first
guard let windowScene = scene as? UIWindowScene else { return 0 }
guard let window = windowScene.windows.first else { return 0 }
return window.safeAreaInsets.top
} else if #available(iOS 11.0, *) {
guard let window = UIApplication.shared.windows.first else { return 0 }
return window.safeAreaInsets.top
}
return 0;
}
/// 底部安全區(qū)高度
static func vg_safeDistanceBottom() -> CGFloat {
if #available(iOS 13.0, *) {
let scene = UIApplication.shared.connectedScenes.first
guard let windowScene = scene as? UIWindowScene else { return 0 }
guard let window = windowScene.windows.first else { return 0 }
return window.safeAreaInsets.bottom
} else if #available(iOS 11.0, *) {
guard let window = UIApplication.shared.windows.first else { return 0 }
return window.safeAreaInsets.bottom
}
return 0;
}
/// 頂部狀態(tài)欄高度(包括安全區(qū))
static func vg_statusBarHeight() -> CGFloat {
var statusBarHeight: CGFloat = 0
if #available(iOS 13.0, *) {
let scene = UIApplication.shared.connectedScenes.first
guard let windowScene = scene as? UIWindowScene else { return 0 }
guard let statusBarManager = windowScene.statusBarManager else { return 0 }
statusBarHeight = statusBarManager.statusBarFrame.height
} else {
statusBarHeight = UIApplication.shared.statusBarFrame.height
}
return statusBarHeight
}
/// 導(dǎo)航欄高度
static func vg_navigationBarHeight() -> CGFloat {
return 44.0
}
/// 狀態(tài)欄+導(dǎo)航欄的高度
static func vg_navigationFullHeight() -> CGFloat {
return UIDevice.vg_statusBarHeight() + UIDevice.vg_navigationBarHeight()
}
/// 底部導(dǎo)航欄高度
static func vg_tabBarHeight() -> CGFloat {
return 49.0
}
/// 底部導(dǎo)航欄高度(包括安全區(qū))
static func vg_tabBarFullHeight() -> CGFloat {
return UIDevice.vg_tabBarHeight() + UIDevice.vg_safeDistanceBottom()
}
}