仅显示iPhone X的状态栏

时间:2017-10-10 22:58:12

标签: swift xcode iphone-x

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

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

2 个答案:

答案 0 :(得分:5)

方法1:

您必须将此值添加到plist:" 查看基于控制器的状态栏外观"并将其设置为" "。 enter image description here

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

方法2: " 查看基于控制器的状态栏外观"并将其设置为" "。 默认情况下。

与iOS11 + setStatusBarHidden&不推荐使用isStatusBarHidden, 可以从iOS7 +获取prefersStatusBarHidden,我们可以在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
    }

在ViewController.Swift中

override var prefersStatusBarHidden: Bool {
        return StatusBarInfo.isToHiddenStatus
    }

答案 1 :(得分:0)

在此处查找完整帖子: 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"
}