仅显示 iPhone X 的状态栏

Show Status Bar only for iPhone X

如果不是 iPhone X,希望隐藏状态栏,如果是 iPhone X,则显示状态栏。

这很可能必须以编程方式完成,因为在 plist 中没有支持此功能的密钥(我找到的最接近的是 UIStatusBarHidden

在此处找到完整的 post: How to get device make and model on iOS?

这是获取模型类型的函数:

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

然后像这样进行验证

override var prefersStatusBarHidden: Bool {
  return  UIDevice.current.modelName == "iPhone X"
}

方法一:

您必须将此值添加到 plist:“View controller-based status bar appearance”并将其设置为“NO” .

然后在 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
    }

方法二:查看基于控制器的状态栏外观”并将其设置为“YES”。 这是默认设置。

如 iOS11+ setStatusBarHiddenisStatusBarHidden 已弃用, prefersStatusBarHidden 可从 iOS7+ 获得,我们可以在 ViewController 上进行状态栏可见性设置,因为 -

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
    }

在ViewController.Swift

override var prefersStatusBarHidden: Bool {
        return StatusBarInfo.isToHiddenStatus
    }