适配原因:由于新出现的刘海屏,导致状态栏高度和底部安全区域的高度变得不确定了; 在
iOS 13.0
之后,UIApplication
中的statusBarFrame
属性被废弃⚠️了。官方提供了UIStatusBarManager
类中的statusBarFrame
属性来进行获取,具体代码如下:
#pragma mark- 顶部状态栏+导航栏
- (CGFloat)setupSafeTopHeight {
CGFloat navHeight = 44; // 导航栏固定高度
CGFloat statusHeight = 20; // 状态栏高度
if (@available(iOS 13.0, *)) {
NSSet *set = [UIApplication sharedApplication].connectedScenes;
UIWindowScene *scene = [set anyObject];
UIStatusBarManager *statusBarManager = scene.statusBarManager;
statusHeight = statusBarManager.statusBarFrame.size.height;
} else {
statusHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
}
return navHeight + statusHeight;
}
#pragma mark- 底部导航栏+底部安全区域
- (CGFloat)setupSafeBottomHeight {
CGFloat height = 49; // 底部导航栏固定高度
if (@available(iOS 13.0, *)) {
NSSet *set = [UIApplication sharedApplication].connectedScenes;
UIWindowScene *scene = [set anyObject];
UIWindow *window = scene.windows.firstObject;
height = height + window.safeAreaInsets.bottom;
} else if (@available(iOS 11.0, *)) {
UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
height = height + window.safeAreaInsets.bottom;
}
return height;
}
网友评论