Search code examples
swiftxcodeiphone-x

Show Status Bar only for iPhone X


Looking to hide the status bar if it is not iPhone X and show the status bar if it is iPhone X.

Most likely this will have to be done programmatically since there is no key that supports this functionality in the plist (closest one I found is UIStatusBarHidden)


Solution

  • Method 1:

    You have to add this value to plist: "View controller-based status bar appearance" and set it to "NO". enter image description here

    After that add this in AppDelegate

       var window: UIWindow?
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
            window = UIWindow(frame: UIScreen.main.bounds)
            if #available(iOS 11.0, *) {
                if (window?.safeAreaInsets.top)! > CGFloat(0.0) || window?.safeAreaInsets != .zero {
                    print("iPhone X")
                    application.isStatusBarHidden = false
                    //or UIApplication.shared.isStatusBarHidden = true
                }
                else {
                    print("Not iPhone X")
                    application.isStatusBarHidden = true
                }
            }
            return true
        }
    

    Method 2: "View controller-based status bar appearance" and set it to "YES". Which is by default.

    As in iOS11+ setStatusBarHidden & isStatusBarHidden are deprecated, prefersStatusBarHidden is available from iOS7+, We can make status bar visibility settings over ViewController as-

    struct StatusBarInfo {
        static var isToHiddenStatus = false
      }
        var window: UIWindow?
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
            if #available(iOS 11.0, *) {
                if (window?.safeAreaInsets.top)! > CGFloat(0.0) || window?.safeAreaInsets != .zero {
                    print("iPhone X")
                    StatusBarInfo.isToHiddenStatus = false
                }
                else {
                    StatusBarInfo.isToHiddenStatus = true
                    print("Not iPhone X")
                }
            }
            return true
        }
    

    In ViewController.Swift

    override var prefersStatusBarHidden: Bool {
            return StatusBarInfo.isToHiddenStatus
        }