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)

StevenTsooo
  • 498
  • 4
  • 13
  • based on this: https://stackoverflow.com/questions/11197509/how-to-get-device-make-and-model-on-ios you can do the validation, and then just hide it if its that make – arvidurs Oct 10 '17 at 23:08

2 Answers2

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
    }
Jack
  • 13,571
  • 6
  • 76
  • 98
  • @IulianOnofrei `isStatusBarHidden` if both `get` & `set` you should check [this apple doc](https://developer.apple.com/documentation/uikit/uiapplication/1622982-isstatusbarhidden) – Jack Oct 20 '17 at 08:07
  • Weird, [this apple doc](https://developer.apple.com/documentation/uikit/uiapplication/1622982-statusbarhidden?language=objc) says it's readonly. – Iulian Onofrei Oct 20 '17 at 08: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. – Iulian Onofrei Oct 20 '17 at 08: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"
}
arvidurs
  • 2,853
  • 4
  • 25
  • 36
  • This is also wrong the modelName returned for iPhone X should be `"iPhone10,3" (CDMA)` or `"iPhone10,6" (GSM)` – Leo Dabus Oct 11 '17 at 02:12