Skip to content

Instantly share code, notes, and snippets.

@software-mariodiana
Last active October 10, 2024 23:44
Show Gist options
  • Save software-mariodiana/12c917c910981e06215bde0d91fb2b1a to your computer and use it in GitHub Desktop.
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.
#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