iOS: Detect if the device is iPhone X family (frameless)
Swift 5, iOS 14 supported
Thanks to @Tanin and @DominicMDev, since keyWindow was deprecated in iOS 13
and the iPad Pro has non-zero safeAreaInsets
, this works fine for me.
(Already tested on iPhone 8
, iPhone 11 Pro
and iPad Pro (11-inch)(2nd gen)
Simulators)
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 } }}
Since keyWindow was deprecated in iOS 13
, based on the solution to find keyWindow
from here, this one works for me
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 }}