在我的应用程序中,有一些无框设备的逻辑(iPhoneX,Xs Xs max,Xr)。目前它基于设备的模型工作,因此,我通过DeviceKit框架检测模型。
但我想将此逻辑扩展到未来的无框设备。可能在一年内我们将有一些额外的无框设备。那么,我如何检测设备是否无框架?它应该涵盖所有当前的无框设备和未来的设备。
我们不能依赖faceID,safeAreaInset,屏幕高度或大小。那么,那又怎样?
您可以“过滤”顶级,如:
var hasTopNotch: Bool {
if #available(iOS 11.0, tvOS 11.0, *) {
return UIApplication.shared.delegate?.window??.safeAreaInsets.top ?? 0 > 20
}
return false
}
这样你就可以涵盖所有的方向:
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
}
这适用于任何方向。因为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
}
}