之前的获得状态栏高度代码是

func kStatusBarHeight() -> CGFloat {
    var statusBarHeight: CGFloat = 0
    if #available(iOS 13.0, *) {
        let statusManager = UIApplication.shared.windows.first?.windowScene?.statusBarManager
        statusBarHeight =  statusManager?.statusBarFrame.height ?? 20.0
    } else {
        statusBarHeight = UIApplication.shared.statusBarFrame.height
    }
    return statusBarHeight
}

 

上面的代码,iPhone14之前手机获得状态栏高度都是正确的,刘海屏也没问题,

但是灵动岛手机有问题,获得高度是44是错误的。iPhone14pro iPhone14pro max状态栏高度应该是56。

应该用下面的代码解决这问题

func kStatusBarHeight() -> CGFloat {
    var statusBarHeight: CGFloat = 0
    if #available(iOS 13.0, *) {
        let window = UIApplication.shared.windows.first
        let topPadding = window?.safeAreaInsets.top
        statusBarHeight = topPadding ?? 20.0
    } else {
        statusBarHeight = UIApplication.shared.statusBarFrame.height
    }
    return statusBarHeight
}