Ben*_*ato 22
你可以"fitler"获得最高级别,例如:
var hasTopNotch: Bool {
if #available(iOS 11.0, tvOS 11.0, *) {
return UIApplication.shared.delegate?.window??.safeAreaInsets.top ?? 0 > 20
}
return false
}
Run Code Online (Sandbox Code Playgroud)
Saa*_*afo 16
支持 Swift 5,iOS 14
感谢 @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
}
}
}
Run Code Online (Sandbox Code Playgroud)
Tan*_*nin 10
因为keyWindow was deprecated in iOS 13,基于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
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
这对任何方向都有效。无需担心 11.0 之前的 iOS 版本,因为 iPhone X 最低版本为 11.0。来源
extension UIDevice {
var hasNotch: Bool {
if #available(iOS 11.0, *) {
return UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0 > 0
}
return false
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3793 次 |
| 最近记录: |