How to Get the Status Bar Height in iOS 13

How to get the status bar height in iOS 13?

As the warning hints, you can access the statusBarManager which has a statusBarFrame property. This is defined on your UIWindow's windowScene.

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

Status bar height in Swift

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

iOS 13 setting status bar background color

Kuray just provided me a solution here:

https://freakycoder.com/ios-notes-13-how-to-change-status-bar-color-1431c185e845

Add the below to viewdidload. Please head over to his medium post and give him a few claps!

if #available(iOS 13.0, *) {
let app = UIApplication.shared
let statusBarHeight: CGFloat = app.statusBarFrame.size.height

let statusbarView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: statusBarHeight))
statusbarView.backgroundColor = UIColor.red
view.addSubview(statusbarView)
} else {
let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
statusBar?.backgroundColor = UIColor.red
}

iOS - Getting Status bar height & width 0 in landscape mode

We can achieve this by using

window.safeAreaLayoutGuide.layoutFrame

For Ex:

let safeFrame = window.safeAreaLayoutGuide.layoutFrame;
let safeFrameHeight = safeFrame.origin.y;
let safeFrameWidth = safeFrame.origin.x;


Related Topics



Leave a reply



Submit