Status bar height in Swift

Oleshko picture Oleshko · Sep 22, 2014 · Viewed 59.8k times · Source

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

In Objective-C, it's like this:

[UIApplication sharedApplication].statusBarFrame.size.height.

Answer

Kirsteins picture Kirsteins · Sep 22, 2014

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