Skip to content

Instantly share code, notes, and snippets.

@badlands
Last active May 24, 2019 12:05
Show Gist options
  • Save badlands/7a706761ed77c652ede100619aba69fd to your computer and use it in GitHub Desktop.
Save badlands/7a706761ed77c652ede100619aba69fd to your computer and use it in GitHub Desktop.
UIDeviceExtension: hasNotch
import Foundation
import UIKit
extension UIDevice {
/// Returns 'true' if the current device has a notch
var hasNotch: Bool {
if #available(iOS 11.0, *) {
// Case 1: Portrait && top safe area inset >= 44
let case1 = !UIDevice.current.orientation.isLandscape && (UIApplication.shared.keyWindow?.safeAreaInsets.top ?? 0) >= 44
// Case 2: Lanscape && left/right safe area inset > 0
let case2 = UIDevice.current.orientation.isLandscape && ((UIApplication.shared.keyWindow?.safeAreaInsets.left ?? 0) > 0 || (UIApplication.shared.keyWindow?.safeAreaInsets.right ?? 0) > 0)
return case1 || case2
} else {
// Fallback on earlier versions
return false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment