当前位置: 代码网 > 科技>操作系统>Windows > swift 获取window

swift 获取window

2024年07月28日 Windows 我要评论
【代码】swift 获取window。

获取当前window


   // mark: - 当前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()
    }
(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com