如何以编程方式获取iOS状态栏高度

288

我知道目前iPhone/iPad的状态栏(包括时间、电池和网络连接)在非Retina屏幕上为20像素,在Retina屏幕上为40像素,但为了未来使我的应用更具可靠性,我希望能够在不硬编码数值的情况下确定此高度。是否可能通过编程方式来计算状态栏的高度?

16个回答

2

以下是一种使用Swift获取屏幕状态栏高度的方法:

var screenStatusBarHeight: CGFloat {
    return UIApplication.sharedApplication().statusBarFrame.height
}

这些功能已经作为我的项目的标准函数包含在内:https://github.com/goktugyil/EZSwiftExtensions


(注:该项目中包含了相关的IT技术内容,但具体是什么需要进一步了解。)

0

Xcode 15 可用

如果你使用的是 UIKit,不确定 SwiftUI 是否也适用,你可以在 SceneDelegate.swift 中访问到这个变量 scene,就像你在这里看到的一样。

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

        guard let scene = (scene as? UIWindowScene) else { return }

    }
    
   ...

}

您可以通过以下方式访问状态栏的高度:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

        guard let scene = (scene as? UIWindowScene) else { return }

        // Use this as to your needs.
        let height = scene.statusBarManager?.statusBarFrame.height ?? 0
    }

   ...

}

0

1
除非不是这样。请参见上文。 - Martin-Gilles Lavoie

0

Swift 5

UIApplication.shared.statusBarFrame.height


3
请提供一些背景或者解释,说明为什么这个回答是针对 OP 问题的。 - António Ribeiro

0

我刚刚发现了一种方法,可以让你不直接访问状态栏高度,而是计算它。

导航栏高度 - 顶部布局指南长度 = 状态栏高度

Swift:

let statusBarHeight = self.topLayoutGuide.length-self.navigationController?.navigationBar.frame.height

self.topLayoutGuide.length 是被半透明导航栏覆盖的顶部区域,而 self.navigationController?.navigationBar.frame.height 是不包括状态栏的半透明导航栏高度,通常为44pt。因此,通过使用这种方法,您可以轻松计算状态栏的高度,而不必担心由于电话呼叫而导致状态栏高度的变化。


这个不行,self.topLayoutGuide.length 是0,所以结果最终变成了-44。 - nambatee
@nambatee 我认为它不再起作用是因为我使用的是iOS 11,Apple使用了safeAreaInsets。 - Henry Ngan

-6
使用以下单行代码,您可以获取任何方向上的状态栏高度,并且还可以判断它是否可见。
#define STATUS_BAR_HIGHT (
    [UIApplicationsharedApplication].statusBarHidden ? 0 : (
        [UIApplicationsharedApplication].statusBarFrame.size.height > 100 ?
            [UIApplicationsharedApplication].statusBarFrame.size.width :
            [UIApplicationsharedApplication].statusBarFrame.size.height
    )
)

这只是一个简单但非常有用的宏,尝试一下,您不需要编写任何额外的代码


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接