Status bar height in Swift

2019-03-09 05:32发布

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

In Objective-C, it's like this:

[UIApplication sharedApplication].statusBarFrame.size.height.

4条回答
对你真心纯属浪费
2楼-- · 2019-03-09 06:10

Is there any problems with Swift 2.x:

UIApplication.sharedApplication().statusBarFrame.size.height

Swift 3 or Swift 4:

UIApplication.shared.statusBarFrame.height

Make sure UIKit is imported

import UIKit
查看更多
Deceive 欺骗
3楼-- · 2019-03-09 06:10

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
查看更多
Viruses.
4楼-- · 2019-03-09 06:21

Its just like in Objective-C:

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

This is included as a standard variable in:

https://github.com/goktugyil/EZSwiftExtensions

查看更多
Rolldiameter
5楼-- · 2019-03-09 06:35

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

let app = UIApplication.sharedApplication()
let height = app.statusBarFrame.size.height
查看更多
登录 后发表回答