Status bar height in Swift
73,607
Solution 1
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
In iOS 13, you will get a deprecated warning"
'statusBarFrame' was deprecated in iOS 13.0: Use the statusBarManager property of the window scene instead.
To fix this:
let height = view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
Solution 2
Updated Answer Supporting iOS 13+ and older iOS Versions for Swift 5
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
}
Happy Coding!
Solution 3
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 {
let viewController = UIApplication.shared.windows.first!.rootViewController
return viewController!.view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
}
}
Then you can do:
Screen.statusBarHeight
Solution 4
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
Solution 5
Reworked answer from Ibrahim :
extension UIApplication {
static var statusBarHeight: CGFloat {
if #available(iOS 13.0, *) {
let window = shared.windows.filter { $0.isKeyWindow }.first
return window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
}
return shared.statusBarFrame.height
}
}
Related videos on Youtube
Author by
Oleshko
Updated on January 14, 2022Comments
-
Oleshko over 2 years
How can I get the status bar's height programmatically in Swift?
In Objective-C, it's like this:
[UIApplication sharedApplication].statusBarFrame.size.height.
-
DoK over 7 yearsNew syntax for Swift 3:
UIApplication.shared.statusBarFrame.size.height
-
joern over 7 yearsYou can omit
size
in Swift 3:UIApplication.shared.statusBarFrame.height
is enough. -
Invincible_Pain about 4 yearslet height = view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0 is returning 0 actually
-
Karan Pal about 4 years@Invincible_Pain it is probably because your current window has not been loaded yet, so replace
view.window?
withUIApplication.shared.keyWindow?
-
Peter Suwara about 4 yearsA little tweak, make it a static var, then access is much clearer ie. UIApplication.statusBarHeight
-
Md. Ibrahim Hassan about 4 yearsIt is actually implemented as a global function. Making a static var would be convenient if I wrap it up inside a class.
-
Raymond about 4 yearslet height = view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0 doesn't work any more