获取当前window
class func keyWindow() -> UIWindow {
if #available(iOS 15.0, *) {
let keyWindow = UIApplication.shared.connectedScenes
.map({ $0 as? UIWindowScene })
.compactMap({ $0 })
.first?.windows.first ?? UIWindow()
return keyWindow
}else {
let keyWindow = UIApplication.shared.windows.first ?? UIWindow()
return keyWindow
}
}
安全区域,状态、导航、TabBar高度
static func xp_safeDistanceTop() -> CGFloat {
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
}
static func xp_safeDistanceBottom() -> CGFloat {
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
}
static func xp_statusBarHeight() -> CGFloat {
var statusBarHeight: CGFloat = 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
return statusBarHeight
}
static func xp_navigationBarHeight() -> CGFloat {
return 44.0
}
static func xp_navigationFullHeight() -> CGFloat {
return UIDevice.xp_statusBarHeight() + UIDevice.xp_navigationBarHeight()
}
static func xp_tabBarHeight() -> CGFloat {
return 49.0
}
static func xp_tabBarFullHeight() -> CGFloat {
return UIDevice.xp_tabBarHeight() + UIDevice.xp_safeDistanceBottom()
}