ios Swift 中的状态栏高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25973733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:42:15  来源:igfitidea点击:

Status bar height in Swift

iosswiftstatusbar

提问by Oleshko

How can I get the status bar's height programmatically in Swift?

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

In Objective-C, it's like this:

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

[UIApplication sharedApplication].statusBarFrame.size.height.

回答by Kirsteins

Is there any problems with Swift 2.x:

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

UIApplication.sharedApplication().statusBarFrame.size.height

Swift 3 or Swift 4:

Swift 3 或 Swift 4

UIApplication.shared.statusBarFrame.height

Make sure UIKitis imported

确保UIKit是进口的

import UIKit

In iOS 13, you will get a deprecated warning"

在 iOS 13 中,您将收到已弃用的警告”

'statusBarFrame' was deprecated in iOS 13.0: Use the statusBarManager property of the window scene instead.

'statusBarFrame' 在 iOS 13.0 中被弃用:改用窗口场景的 statusBarManager 属性。

To fix this:

要解决此问题:

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

回答by vcsjones

Swift is just a different language. The API elements are the same. Perhaps something like this:

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

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

回答by Bobby

This is what I use:

这是我使用的:

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
    }
}

Then you can do:

然后你可以这样做:

Screen.statusBarHeight

回答by Md. Ibrahim Hassan

Updated Answer Supporting iOS 13+ and older iOS Versions for Swift 5

更新的答案支持 Swift 5 的 iOS 13+ 和更旧的 iOS 版本

 func getStatusBarHeight() -> CGFloat {
    var statusBarHeight: CGFloat = 0
    if #available(iOS 13.0, *) {
        let window = UIApplication.shared.windows.filter {
var screenStatusBarHeight: CGFloat {
    return UIApplication.sharedApplication().statusBarFrame.height
}
.isKeyWindow}.first statusBarHeight = window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0 } else { statusBarHeight = UIApplication.shared.statusBarFrame.height } return statusBarHeight }

Happy Coding!

快乐编码!

回答by Esqarrouth

Its just like in Objective-C:

就像在 Objective-C 中一样:

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

This is included as a standard variable in:

这作为标准变量包含在:

https://github.com/goktugyil/EZSwiftExtensions

https://github.com/goktugyil/EZSwiftExtensions

回答by Peter Suwara

Reworked answer from Ibrahim :

易卜拉欣的重做答案:

##代码##