SwiftUI自定义获取ScreenWidth 、ScreenHeight、StatusBarHeight、NavigationBarHeight、TabBarHeight、BottomSafeHeight的快捷方法
//
// LKLayout.swift
// LKSwiftUIGradient
//
// Created by 李棒棒 on 2023/12/19.
//
import SwiftUI
struct LKLayout {
/// 屏幕高
public static let screen_h = UIScreen.main.bounds.height
/// 屏幕宽
public static let screen_w = UIScreen.main.bounds.width
///导航状态栏高度
public static let statusBar_height = LKLayout().NAV_STATUSBAR_HEIGHT()
///底部导航菜单的指示条高度(屏幕底部安全距离)
public static let bottomSafe_height = LKLayout().TABBAR_INDICATOR_HEIGHT()
/// 导航栏内容高度
public static let navBarContent_height = 44.0
///导航栏整体高度 状态栏高 + 导航栏内容高
public static let navBar_height = (statusBar_height + navBarContent_height)
///底部导航菜单高度 内容高 + 底部指示条高度
public static let tabBar_height = (49.0 + bottomSafe_height)
//状态栏高度
private func NAV_STATUSBAR_HEIGHT() -> CGFloat {
if #available(iOS 13.0, *) {
return lkWindow()?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
} else {
return UIApplication.shared.statusBarFrame.height
}
}
//底部指示条
private func TABBAR_INDICATOR_HEIGHT() -> CGFloat {
if #available(iOS 11.0, *) {
return lkWindow()?.safeAreaInsets.bottom ?? 0
} else {
return 0
}
}
//获取window
private func lkWindow() -> UIWindow? {
if #available(iOS 13.0, *) {
let winScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
return winScene?.windows.first
} else {
return UIApplication.shared.delegate?.window ?? UIApplication.shared.keyWindow
}
}
}
BangBang
BangBang