Skip to content

Instantly share code, notes, and snippets.

@rgkobashi
Created September 30, 2018 07:53
Show Gist options
  • Save rgkobashi/298a6016662c64a07a12f78a5b6e73ea to your computer and use it in GitHub Desktop.
Save rgkobashi/298a6016662c64a07a12f78a5b6e73ea to your computer and use it in GitHub Desktop.
Hide/Show status bar iOS Swift
// The methods of Appearance.StatusBar can be used to hide and show the status bar on iOS.
// As mention in other places, this is more like hack than a solution. I personally don't like it.
// However if on your application you are making use of navigationBar or similar components,
// hidding the statusBar on the right way (overriding `prefersStatusBarHidden`) will mess with your constraints.
// For more details please refer to this answer and its comments: https://stackoverflow.com/a/44066303/8483739
struct Appearance {
private init() {}
}
extension Appearance {
struct StatusBar {
private static weak var application: UIApplication!
private init() {}
static func setup(_ application: UIApplication) {
self.application = application
}
static func hide() {
application.keyWindow?.windowLevel = UIWindowLevelStatusBar
}
static func show() {
application.keyWindow?.windowLevel = UIWindowLevelNormal
}
}
}
// MARK: - To set it up
class AppDelegate: UIResponder, UIApplicationDelegate {
//....
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//....
setGlobalAppearance(application)
//....
}
}
extension AppDelegate {
private func setGlobalAppearance(_ application: UIApplication) {
Appearance.StatusBar.setup(application)
//....
}
}
// MARK: - To use it
Appearance.StatusBar.hide()
Appearance.StatusBar.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment