随着挖孔屏的出现原来的计算方法就出bug了。现重新整理如下:
@interface UIDevice (Addition)
/// 顶部安全区高度
+ (CGFloat)safeDistanceTop;
/// 底部安全区高度
+ (CGFloat)safeDistanceBottom;
/// 顶部状态栏高度(包括安全区)
+ (CGFloat)statusBarHeight;
/// 导航栏高度
+ (CGFloat)navigationBarHeight;
/// 状态栏+导航栏的高度
+ (CGFloat)navigationFullHeight;
/// 底部导航栏高度
+ (CGFloat)tabBarHeight;
/// 底部导航栏高度(包括安全区)
+ (CGFloat)tabBarFullHeight;
@end
#import "UIDevice+Addition.h"
@implementation UIDevice (Addition)
/// 顶部安全区高度
+ (CGFloat)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)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)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)navigationBarHeight {
return 44.0f;
}
/// 状态栏+导航栏的高度
+ (CGFloat)navigationFullHeight {
return [UIDevice safeDistanceTop] + [UIDevice navigationBarHeight];
}
/// 底部导航栏高度
+ (CGFloat)tabBarHeight {
return 49.0f;
}
/// 底部导航栏高度(包括安全区)
+ (CGFloat)tabBarFullHeight {
return [UIDevice statusBarHeight] + [UIDevice safeDistanceBottom];
}
@end
import UIKit
extension UIDevice {
/// 顶部安全区高度
static func ch_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 ch_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 ch_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 ch_navigationBarHeight() -> CGFloat {
return 44.0
}
/// 状态栏+导航栏的高度
static func ch_navigationFullHeight() -> CGFloat {
return UIDevice.ch_statusBarHeight() + UIDevice.ch_navigationBarHeight()
}
/// 底部导航栏高度
static func ch_tabBarHeight() -> CGFloat {
return 49.0
}
/// 底部导航栏高度(包括安全区)
static func ch_tabBarFullHeight() -> CGFloat {
return UIDevice.ch_tabBarHeight() + UIDevice.ch_safedistanceBottom()
}
}
网友评论