状态栏高度(灵动岛也适用)
使用xcode14后,使用statusBarFrame获取系统状态栏高度失效,无法适配iPhone14系列
注:各机型系列状态栏高度
- 非刘海屏机型:20px
- iPhone 11 / XR:48px
- iPhone12,13非mini系列 / 14:47px
- iPhone12,13mini系列:50px
- iPhone 14、15 Pro/14 、15Pro Max:59px
- 其他刘海屏系列:44px
代码:
OC获取代码
CGFloat height = 0.0;//最终高度存储容器
if (@available(iOS 13.0, *)) {
CGFloat topHeight = [UIApplication sharedApplication].windows.firstObject.safeAreaInsets.top;
height = topHeight ? topHeight : 20.0;
}else {
height = [[UIApplication sharedApplication] statusBarFrame].size.height;
}
swift获取代码
var height = 0.0
if #available(iOS 13.0, *) {
let topHeight = UIApplication.shared.windows.first?.safeAreaInsets.top
height = topHeight ?? 20
}else{
height = UIApplication.shared.statusBarFrame.size.height
}
使用宏定义(OC)
//statusbar的高度
#define STATUS_HEIGHT \
({\
CGFloat height = 0.0;\
if (@available(iOS 13.0, *)) {\
CGFloat topHeight = [UIApplication sharedApplication].windows.firstObject.safeAreaInsets.top;\
height = topHeight ? topHeight : 20.0;\
}else {\
height = [[UIApplication sharedApplication] statusBarFrame].size.height;\
}\
(height);\
})\
//底部安全高度
#define BOTTOM_HEIGHT \
({\
CGFloat height = 0.0;\
if (@available(iOS 13.0, *)) {\
NSSet *set = [UIApplication sharedApplication].connectedScenes;\
UIWindowScene *windowScene = [set anyObject];\
UIWindow *window = windowScene.windows.firstObject;\
height = window.safeAreaInsets.bottom;\
} else if (@available(iOS 11.0, *)) {\
UIWindow *window = [UIApplication sharedApplication].windows.firstObject;\
height = window.safeAreaInsets.bottom;\
}\
(height);\
})\