Last active
May 24, 2019 12:05
-
-
Save badlands/7a706761ed77c652ede100619aba69fd to your computer and use it in GitHub Desktop.
UIDeviceExtension: hasNotch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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