美文网首页Swift开发学习我的Swift开发
iOS开发笔记-126:swift5 对Iphone手机状态栏高

iOS开发笔记-126:swift5 对Iphone手机状态栏高

作者: 原味蛋炒饭 | 来源:发表于2023-03-22 16:05 被阅读0次
let kStatusBarHeight        : CGFloat = UIDevice.vg_statusBarHeight()
let kStatusBarSpaceX        : CGFloat = UIDevice.vg_safeDistanceTop()
let kTopBarHeight           : CGFloat = UIDevice.vg_navigationBarHeight()//44
let kNavigationBar64        : CGFloat = UIDevice.vg_navigationFullHeight()
let kBottomBarHeight        : CGFloat = UIDevice.vg_tabBarFullHeight()
let kBottomBarRealHeight    : CGFloat = UIDevice.vg_tabBarHeight()//49
let kTabbarSafeBottomMargin : CGFloat = UIDevice.vg_safeDistanceBottom()


extension UIDevice {
    
    /// 顶部安全区高度
    static func vg_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 vg_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 vg_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 vg_navigationBarHeight() -> CGFloat {
        return 44.0
    }
    
    /// 状态栏+导航栏的高度
    static func vg_navigationFullHeight() -> CGFloat {
        return UIDevice.vg_statusBarHeight() + UIDevice.vg_navigationBarHeight()
    }
    
    /// 底部导航栏高度
    static func vg_tabBarHeight() -> CGFloat {
        return 49.0
    }
    
    /// 底部导航栏高度(包括安全区)
    static func vg_tabBarFullHeight() -> CGFloat {
        return UIDevice.vg_tabBarHeight() + UIDevice.vg_safeDistanceBottom()
    }
}


相关文章

网友评论

    本文标题:iOS开发笔记-126:swift5 对Iphone手机状态栏高

    本文链接:https://www.haomeiwen.com/subject/ljqbrdtx.html