xcode 仅显示 iPhone X 的状态栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46677240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:21:22  来源:igfitidea点击:

Show Status Bar only for iPhone X

swiftxcodeiphone-x

提问by StevenTsooo

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

如果不是 iPhone X,希望隐藏状态栏,如果是 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)

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

回答by Hyman

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

您必须将此值添加到 plist:“查看基于控制器的状态栏外观”并将其设置为“ NO”。 在此处输入图片说明

After that add this in AppDelegate

之后添加这个 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& isStatusBarHiddenare deprecated, prefersStatusBarHiddenis available from iOS7+, We can make status bar visibility settings over ViewControlleras-

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

In ViewController.Swift

在 ViewController.Swift 中

override var prefersStatusBarHidden: Bool {
        return StatusBarInfo.isToHiddenStatus
    }

回答by arvidurs

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

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