问题
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:
Method 1:
You have to add this value to plist: "View controller-based status bar appearance" and set it to "NO".
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
}
回答2:
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"
}
来源:https://stackoverflow.com/questions/46677240/show-status-bar-only-for-iphone-x