在我的应用中,无框设备(iPhoneX,Xs Xs max,Xr)有一些逻辑。当前,它基于设备的模型工作,因此,我通过DeviceKit框架检测了该模型。
但是我想将此逻辑扩展到未来的无框设备。大概一年后,我们将有一些额外的无框设备。因此,如何检测设备是否无框架?它应该涵盖当前的所有无框设备和将来的无框设备。
我们不能依靠faceID,safeAreaInset,屏幕高度或大小。那又如何呢?
答案 0 :(得分:16)
您可以“拟合”最佳状态,例如:
var hasTopNotch: Bool {
if #available(iOS 11.0, tvOS 11.0, *) {
return UIApplication.shared.delegate?.window??.safeAreaInsets.top ?? 0 > 20
}
return false
}
答案 1 :(得分:2)
extension UIDevice {
var hasNotch: Bool
{
if #available(iOS 11.0, *)
{
let bottom = UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0
return bottom > 0
} else
{
// Fallback on earlier versions
return false
}
}
}
使用
if UIDevice.current.hasNotch
{
//... consider notch
}
else
{
//... don't have to consider notch
}
答案 2 :(得分:2)
自keyWindow was deprecated in iOS 13
起,根据从here中找到keyWindow
的解决方案开始,这一点对我有效
extension UIDevice {
var hasNotch: Bool {
if #available(iOS 11.0, *) {
let keyWindow = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
return keyWindow?.safeAreaInsets.bottom ?? 0 > 0
}
return false
}
}
答案 3 :(得分:1)
快捷键5
var hasNotch: Bool {
if #available(iOS 11.0, tvOS 11.0, *) {
let bottom = UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0
return bottom > 0
} else {
return false
}
}
答案 4 :(得分:1)
支持iOS 5的Swift 5
感谢@Tanin和@DominicMDev,由于keyWindow was deprecated in iOS 13
和the iPad Pro has non-zero safeAreaInsets
,这对我来说很好。
(已经在iPhone 8
,iPhone 11 Pro
和iPad Pro (11-inch)(2nd gen)
模拟器上进行过测试)
extension UIDevice {
/// Returns `true` if the device has a notch
var hasNotch: Bool {
guard #available(iOS 11.0, *), let window = UIApplication.shared.windows.filter({$0.isKeyWindow}).first else { return false }
if UIDevice.current.orientation.isPortrait {
return window.safeAreaInsets.top >= 44
} else {
return window.safeAreaInsets.left > 0 || window.safeAreaInsets.right > 0
}
}
}
答案 5 :(得分:0)
这样,您可以覆盖所有方向:
var hasTopNotch: Bool
{
if #available(iOS 11.0, *) {
var safeAreaInset: CGFloat?
if (UIApplication.shared.statusBarOrientation == .portrait) {
safeAreaInset = UIApplication.shared.delegate?.window??.safeAreaInsets.top
}
else if (UIApplication.shared.statusBarOrientation == .landscapeLeft) {
safeAreaInset = UIApplication.shared.delegate?.window??.safeAreaInsets.left
}
else if (UIApplication.shared.statusBarOrientation == .landscapeRight) {
safeAreaInset = UIApplication.shared.delegate?.window??.safeAreaInsets.right
}
return safeAreaInset ?? 0 > 24
}
return false
}
答案 6 :(得分:0)
这对任何方向均有效。 由于iPhone X的最低版本为11.0,因此无需担心11.0之前的iOS版本。 Source
extension UIDevice {
var hasNotch: Bool {
if #available(iOS 11.0, *) {
return UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0 > 0
}
return false
}
}
答案 7 :(得分:0)
我这样做是因为iPadPro具有非零的safeAreaInsets。
extension UIDevice {
/// Returns 'true' if the device has a notch
var hasNotch: Bool {
guard #available(iOS 11.0, *), let window = UIApplication.shared.keyWindow else { return false }
let orientation = UIApplication.shared.statusBarOrientation
if orientation.isPortrait {
return window.safeAreaInsets.top >= 44
} else {
return window.safeAreaInsets.left > 0 || window.safeAreaInsets.right > 0
}
}
}
答案 8 :(得分:-1)
extension UIDevice {
var hasNotch: Bool {
let bottom = UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0
return bottom > 0
}
}
使用 'UIDevice.current.hasNotch' 将返回 true 或 false
斯威夫特 5
var hasNotch: Bool {
let bottom = UIApplication.shared.delegate?.window??.safeAreaInsets.bottom ?? 0
return bottom > 0
}