Swift中的状态栏高度


98

如何在Swift中以编程方式获取状态栏的高度?

在Objective-C中,它是这样的:

[UIApplication sharedApplication].statusBarFrame.size.height.

Answers:


243

Swift 2.x是否有任何问题:

UIApplication.sharedApplication().statusBarFrame.size.height

Swift 3或Swift 4

UIApplication.shared.statusBarFrame.height

确保UIKit已导入

import UIKit

在iOS 13中,您将收到不赞成使用的警告”

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

要解决此问题:

let height = view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0

1
Swift 3的新语法:UIApplication.shared.statusBarFrame.size.height
DoK 2016年

4
您可以size在Swift 3中省略:UIApplication.shared.statusBarFrame.height足够了。
joern

1
谢谢,这对于解决iPhone X上的溢出问题非常有用
Mario Burga,

让height = view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0实际上返回0
Invincible_Pain

@Invincible_Pain可能是因为您当前的窗口尚未加载,请替换view.window?UIApplication.shared.keyWindow?
Karan Pal

5

Swift是另一种语言。API元素相同。也许是这样的:

let app = UIApplication.sharedApplication()
let height = app.statusBarFrame.size.height

5

更新的Answer支持Swift 13的iOS 13+和更旧的iOS版本

 func getStatusBarHeight() -> 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
}

编码愉快!


1
稍作调整,使其成为一个静态变量,然后访问就更加清晰了。UIApplication.statusBarHeight
彼得·苏瓦拉

它实际上是作为全局功能实现的。如果将其包装在类中,则创建静态var将很方便。
Ibrahim Hassan

4

这是我用的:

struct Screen {
    static var width: CGFloat {
        return UIScreen.main.bounds.width
    }
    static var height: CGFloat {
        return UIScreen.main.bounds.height
    }
    static var statusBarHeight: CGFloat {
        return UIApplication.shared.statusBarFrame.size.height
    }
}

然后,您可以执行以下操作:

Screen.statusBarHeight


1

重做易卜拉欣的答案:

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

0

在我的swiftUI项目中,此方法有效。

import UIKit
import SwiftUI

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        let contentView = ContentView()
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            
            if let statusBarHeight = window.windowScene?.statusBarManager?.statusBarFrame.height {
            SceneDelegateDataGetter.shared.height = statusBarHeight
            }
            
            window.rootViewController = HostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }

class SceneDelegateDataGetter {
    static let shared = SceneDelegateDataGetter()
    
    public fileprivate(set) var height: CGFloat = 0
}

使用时

SceneDelegateDataGetter.shared.height
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.