2

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)

1

2 Answers 2

5

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
    }
3
  • @IulianOnofrei isStatusBarHidden if both get & set you should check this apple doc
    – Jack
    Oct 20, 2017 at 8:07
  • Weird, this apple doc says it's readonly. Oct 20, 2017 at 8:08
  • It's the same one, but in Objective-C the getter is called isStatusBarHidden. I just clicked your link and switched the language in top right corner. Oct 20, 2017 at 8:14
0

Find the full post here: How to get device make and model on iOS?

here is the function to get the model type:

extension UIDevice {
    var modelName: String {
        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }
        return identifier
    }
}

then to the validation like this

override var prefersStatusBarHidden: Bool {
  return  UIDevice.current.modelName == "iPhone X"
}
1
  • This is also wrong the modelName returned for iPhone X should be "iPhone10,3" (CDMA) or "iPhone10,6" (GSM)
    – Leo Dabus
    Oct 11, 2017 at 2:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.