如何在iOS 13中获取状态栏的高度?

时间:2019-07-14 01:16:12

标签: ios ios13

在iOS 13中UIApplication.shared.statusBarFrame.height警告

  

在iOS 13.0中不建议使用'statusBarFrame':使用statusBarManager   窗口场景的属性。

如何在不使用iOS 13中已弃用的API的情况下获得状态栏的高度?

8 个答案:

答案 0 :(得分:10)

解决方案:

这似乎在iPhoneX +设备中也没有任何警告地起作用。

迅速4.2 / 5

if #available(iOS 13.0, *) {
    let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
    statusBarHeight = window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
} else {
    statusBarHeight = UIApplication.shared.statusBarFrame.height
}

尝试一下。希望对您有所帮助。

答案 1 :(得分:6)

尝试,我已经尝试过了。

let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first        
let height = window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0

答案 2 :(得分:5)

var statusBarHeight: CGFloat = 0
if #available(iOS 13.0, *) {
    statusBarHeight = UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
} else {
    statusBarHeight = UIApplication.shared.statusBarFrame.height
}

答案 3 :(得分:4)

我遇到了与MMV相同的问题(请参阅他们对Jordan H的回答的评论),view.window?.windowScene?.statusBarManager?.statusBarFrame.height在我的应用启动时返回了nil。具体来说,view.window返回nil。然后,我尝试搜索UIApplication.shared上的windows属性:

for window in UIApplication.shared.windows

在我的情况下,这返回了两个窗口。两个窗口都具有相等的非零状态栏高度。我不确定为什么有两个窗口,但是至少两个窗口的状态栏高度相同。我这样访问状态栏的高度:

if let height = window.windowScene?.statusBarManager?.statusBarFrame.height

在我的案例中,我决定,因为可能存在不同的高度,以使我的代码占据最大的高度。这是我用于应用程序的代码:

let statusBarHeight: CGFloat = {
    var heightToReturn: CGFloat = 0.0
         for window in UIApplication.shared.windows {
             if let height = window.windowScene?.statusBarManager?.statusBarFrame.height, height > heightToReturn {
                 heightToReturn = height
             }
         }
    return heightToReturn
}()

希望这对某人有帮助!

答案 4 :(得分:1)

从这里已经给出的优秀答案中学习,使用 UIApplication 中的 connectScenes,我们可以进行扩展:

extension UIApplication {
    var statusBarHeight: CGFloat {
        connectedScenes
            .compactMap {
                $0 as? UIWindowScene
            }
            .compactMap {
                $0.statusBarManager
            }
            .map {
                $0.statusBarFrame
            }
            .map(\.height)
            .max() ?? 0
    }
}

用法:

let height = UIApplication.shared.statusBarHeight

答案 5 :(得分:0)

作为警告提示,您可以访问具有statusBarManager属性的statusBarFrame。这是在您的UIWindow的{​​{1}}上定义的。

windowScene

答案 6 :(得分:0)

对于iOS 13:

在您的SceneDelegate中:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
 if let windowScene = scene as? UIWindowScene {
    let window = UIWindow(windowScene: windowScene)
    let statusBarSize = windowScene.statusBarManager!.statusBarFrame
    ...// initialize your root view controller
 }
}

,如果要将值传递给View,则可以将值设置为Environment,并在View s中使用它。例如:

首先,我们需要创建我们的环境密钥:

struct StatusBarSizeEnvironmentKey: EnvironmentKey {
   public static let defaultValue: CGRect = CGRect()
}

extension EnvironmentValues {
  public var statusBarSize: CGRect {
    set { self[StatusBarSizeEnvironmentKey.self] = newValue }
    get { self[StatusBarSizeEnvironmentKey] }
  }
}

并在SceneDelegate中设置值:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
 if let windowScene = scene as? UIWindowScene {
    let window = UIWindow(windowScene: windowScene)
    let statusBarSize = windowScene.statusBarManager!.statusBarFrame
    window.rootViewController = UIHostingController(rootView: YourView()
             .environment(\.statusBarSize, statusBarSize))
 }
}

答案 7 :(得分:0)

获取statusBarHeight的最新方法,如下:

    private lazy var statusBarHeight: CGFloat = {
        var statusBarHeight: CGFloat = 0
        if #available(iOS 13.0, *) {
            let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
            statusBarHeight = window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
        } else {
            statusBarHeight = UIApplication.shared.statusBarFrame.height
        }
        return statusBarHeight
    }()