[UIApplication sharedApplication].statusBarFrame.size.height;
但是在 iOS 13.0 之后,UIApplication 单例中的 statusBarFrame 属性被废弃⚠️了。官方希望能我们使用 UIStatusBarManager 类中的 statusBarFrame 熟悉来进行获取,代码如下:
if (@available(iOS 13.0, *)) {
NSSet *set = [UIApplication sharedApplication].connectedScenes;
UIWindowScene *windowScene = [set anyObject];
UIStatusBarManager *statusBarManager = windowScene.statusBarManager;
return statusBarManager.statusBarFrame.size.height;
}
3|代码模版
为了能正确获取系统顶部和底部相关元素(导航栏和安全区)的高度,这里提供了获取顶部和底部安全区、顶部状态栏和导航栏、底部 tabBar。
Objective-C 版本
在 UIDevice+VGAddition.h 中,对各方法进行了声明。
@interface UIDevice (VGAddition)
/// 顶部安全区高度
+ (CGFloat)vg_safedistancetop;
/// 底部安全区高度
+ (CGFloat)vg_safedistanceBottom;
/// 顶部状态栏高度(包括安全区)
+ (CGFloat)vg_statusBarHeight;
/// 导航栏高度
+ (CGFloat)vg_navigationBarHeight;
/// 状态栏+导航栏的高度
+ (CGFloat)vg_navigationFullHeight;
/// 底部导航栏高度
+ (CGFloat)vg_tabBarHeight;
+ (CGFloat)vg_tabBarFullHeight;
@end
在 UIDevice+VGAddition.m 中,对声明的各方法进行了实现。
#import "UIDevice+VGAddition.h"
@implementation UIDevice (VGAddition)
/// 顶部安全区高度
+ (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;
}
/// 底部安全区高度
+ (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;
}
/// 顶部状态栏高度(包括安全区)
+ (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;
}
}
/// 导航栏高度
+ (CGFloat)vg_navigationBarHeight {
return 44.0f;
}
/// 状态栏+导航栏的高度
+ (CGFloat)vg_navigationFullHeight {
return [UIDevice vg_statusBarHeight] + [UIDevice vg_navigationBarHeight];
}
/// 底部导航栏高度
+ (CGFloat)vg_tabBarHeight {
return 49.0f;
}
+ (CGFloat)vg_tabBarFullHeight {
return [UIDevice vg_tabBarHeight] + [UIDevice vg_safedistanceBottom];
}
@end
Swift 版本
UIDevice+VGAddition.swift
extension UIDevice {
/// 顶部安全区高度
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;
}
/// 底部安全区高度
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;
}
/// 顶部状态栏高度(包括安全区)
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
}
/// 导航栏高度
static func vg_navigationBarHeight() -> CGFloat {
return 44.0
}
/// 状态栏+导航栏的高度
static func vg_navigationFullHeight() -> CGFloat {
return UIDevice.vg_statusBarHeight() + UIDevice.vg_navigationBarHeight()
}
/// 底部导航栏高度
static func vg_tabBarHeight() -> CGFloat {
return 49.0
}
static func vg_tabBarFullHeight() -> CGFloat {
return UIDevice.vg_tabBarHeight() + UIDevice.vg_safedistanceBottom()
}
}
网友评论