Created
December 23, 2019 03:18
-
-
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
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
@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 |
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
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!