Last active
October 10, 2024 23:44
-
-
Save software-mariodiana/12c917c910981e06215bde0d91fb2b1a to your computer and use it in GitHub Desktop.
Objective-C method to detect whether the iOS device has a "notch," à la iPhone X.
This file contains hidden or 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 <UIKit/UIKit.h> | |
/** | |
* Return YES if iOS device has a notch; NO, otherwise. | |
*/ | |
- (BOOL)hasDeviceNotch | |
{ | |
if (([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)) { | |
// Only iPhones may have a notch. | |
return NO; | |
} | |
UIWindow* keyWindow = nil; | |
for (id aScene in [[UIApplication sharedApplication] connectedScenes]) { | |
UIWindow* window = [[aScene delegate] window]; | |
if ([window isKeyWindow]) { | |
keyWindow = window; | |
break; | |
} | |
} | |
if (!keyWindow) { | |
NSLog(@"[%@] WARNING: Unable to determine key window! Returning NO.", NSStringFromSelector(_cmd)); | |
return NO; | |
} | |
return [keyWindow safeAreaInsets].bottom > 0.0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment