Skip to content

Instantly share code, notes, and snippets.

@Dirk-Sandberg
Created December 23, 2019 03:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dirk-Sandberg/f5e9e4399002c69003d161e700e943a1 to your computer and use it in GitHub Desktop.
Save Dirk-Sandberg/f5e9e4399002c69003d161e700e943a1 to your computer and use it in GitHub Desktop.
How to determine if the top notch exists on iOS with python/kivy/pyobjus
@interface NotchDetector : UIViewController
@end
@implementation NotchDetector
-(id)init {
NSLog(@"initializing NotchDetector");
return self;
}
- (BOOL)hasTopNotch {
if (@available(iOS 13.0, *)) {
return [self keyWindow].safeAreaInsets.top > 20.0;
}else{
if (@available(iOS 11.0, *)) {
return [[[UIApplication sharedApplication] delegate] window].safeAreaInsets.top > 20.0;
} else {
// Fallback on earlier versions
}
}
return NO;
}
- (UIWindow*)keyWindow {
UIWindow *foundWindow = nil;
NSArray *windows = [[UIApplication sharedApplication]windows];
for (UIWindow *window in windows) {
if (window.isKeyWindow) {
foundWindow = window;
break;
}
}
return foundWindow;
}
@end
from pyobjus import autoclass
from kivy.metrics import dp
from kivy.core.window import Window
class MainApp(MDApp):
iphone_x_notch_height = dp(30)
window_height = NumericProperty(Window.height) # window_height is the height your app should take up (rather than Window.height)
def on_start(self):
notch_detector = autoclass("NotchDetector").alloc().init()
notch_exists = notch_detector.hasTopNotch()
if notch_exists:
self.window_height = Window.height - self.iphone_x_notch_height
MainApp().run()
@kmester
Copy link

kmester commented Mar 14, 2023

Hello there! Do you have a more detailed explanation how to use your NotchDetector?
I would like to scale my window size like this (Kivy to iOS). Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment