Status bar height in Swift

IosSwiftStatusbar

Ios Problem Overview


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

In Objective-C, it's like this:

[UIApplication sharedApplication].statusBarFrame.size.height.

Ios Solutions


Solution 1 - Ios

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

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

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

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

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

Solution 6 - Ios

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

Solution 7 - Ios

I'm working with iOS 15 iPhone (iPad may need some work). The code I use which gets rid of the deprecated warning is as follows:

func getStatusBarHeight() -> CGFloat {
    var statusBarHeight: CGFloat = 0
    let scenes = UIApplication.shared.connectedScenes
    let windowScene = scenes.first as? UIWindowScene
    let window = windowScene?.windows.first
    statusBarHeight = window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
    return statusBarHeight
}

Solution 8 - Ios

On my swiftUI project, this worked.

import UIKit
import SwiftUI

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        let contentView = ContentView()
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            
            if let statusBarHeight = window.windowScene?.statusBarManager?.statusBarFrame.height {
            SceneDelegateDataGetter.shared.height = statusBarHeight
            }
            
            window.rootViewController = HostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }

class SceneDelegateDataGetter {
    static let shared = SceneDelegateDataGetter()
    
    public fileprivate(set) var height: CGFloat = 0
}

When use,

SceneDelegateDataGetter.shared.height

Solution 9 - Ios

From Peter Suwara and Bobby's answers

Updated for Swift 5:

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

struct ScreenUtils {
    static var width: CGFloat {
        return UIScreen.main.bounds.width
    }
    
    static var height: CGFloat {
        return UIScreen.main.bounds.height
    }
    
    static var statusBarHeight: CGFloat {
        return UIApplication.statusBarHeight
    }
}

Using:

ScreenUtils.statusBarHeight

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOleshkoView Question on Stackoverflow
Solution 1 - IosKirsteinsView Answer on Stackoverflow
Solution 2 - IosMd. Ibrahim HassanView Answer on Stackoverflow
Solution 3 - IosBobbyView Answer on Stackoverflow
Solution 4 - IosvcsjonesView Answer on Stackoverflow
Solution 5 - IosPeter SuwaraView Answer on Stackoverflow
Solution 6 - IosEsqarrouthView Answer on Stackoverflow
Solution 7 - IosGIJoeCodesView Answer on Stackoverflow
Solution 8 - IosholaholaView Answer on Stackoverflow
Solution 9 - IosTà TruhoadaView Answer on Stackoverflow