iOS 工具类 - 设备导航栏、安全区域重新设计(使用系统方法兼容灵动岛)

168 阅读2分钟

2025.02.04 工作变动原因,故将一些工作期间Tapd内部写的Wiki文档转移到个人博客。

iPhone灵动岛的新机型,设备头部状态栏的高度比以往又增加了一点,导致项目里旧的兼容写法与设计会有一些出入。 这里重新对Device拓展文件进行重新整理,以兼容灵动岛机型的导航栏、底部安全区域。

1、原来的写法

旧的Device拓展文件,只对刘海屏的机型进行了兼容,而且是以固定浮点数做兼容,这样既不能兼顾旧的调整、也不能兼容新的机型。 tapd_44062861_1714288279_371.png

2、使用系统方法获取导航栏、状态栏、底部安全区域

新的写法使用系统方法去获取,既不会出错、也能兼容以后的新机型发展。

具体的 UIDevice拓展 代码


import Foundation

/// 屏幕宽度
let screenWidth = UIScreen.main.bounds.width
/// 屏幕高度
let screenHeight = UIScreen.main.bounds.height
/// 屏幕宽高比
let screenAspect = UIScreen.main.bounds.width / 375
/// 状态栏高度
let statusBarHeight = UIDevice.yay_statusBarHeight()
/// 导航栏高度
let navBar_Height = UIDevice.yay_navigationFullHeight()
/// 安全区域高度
let safeArea_Bottom = UIDevice.yay_safedistanceBottom()
/// tabBar_Height
let tabBar_Height = UIDevice.yay_tabBarFullHeight()

let invalidHeight = navBar_Height + safeArea_Bottom

extension UIDevice {
    
    /// 顶部安全区高度
    static func yay_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 yay_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 yay_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 yay_navigationBarHeight() -> CGFloat {
        return 44.0
    }
    
    /// 状态栏+导航栏的高度
    static func yay_navigationFullHeight() -> CGFloat {
        return UIDevice.yay_statusBarHeight() + UIDevice.yay_navigationBarHeight()
    }
    
    /// 底部导航栏高度
    static func yay_tabBarHeight() -> CGFloat {
        return 49.0
    }
    
    /// 底部导航栏高度(包括安全区)
    static func yay_tabBarFullHeight() -> CGFloat {
        return UIDevice.yay_tabBarHeight() + UIDevice.yay_safedistanceBottom()
    }
}


最最最后,完结撒花

告辞.jpeg