From e6a68db4c0650c62ee97e5581c9cef02b228bbfe Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Tue, 20 Aug 2019 14:33:19 +0200 Subject: [PATCH] ui/app: (iOS) resize window when the software keyboard is shown Signed-off-by: Elias Naur --- ui/app/os_ios.m | 67 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 14 deletions(-) diff --git a/ui/app/os_ios.m b/ui/app/os_ios.m index 2819dbff..e4757f05 100644 --- a/ui/app/os_ios.m +++ b/ui/app/os_ios.m @@ -9,14 +9,14 @@ #include "os_ios.h" #include "framework_ios.h" -@interface GioViewController : UIViewController -@property UIScreen *screen; -@end - @interface GioView: UIView - (void)setAnimating:(BOOL)anim; @end +@interface GioViewController : UIViewController +@property(weak) UIScreen *screen; +@end + static void redraw(CFTypeRef viewRef, BOOL sync) { UIView *v = (__bridge UIView *)viewRef; CGFloat scale = v.layer.contentsScale; @@ -48,15 +48,18 @@ static void redraw(CFTypeRef viewRef, BOOL sync) { } - (void)applicationDidEnterBackground:(UIApplication *)application { - if (self.window.rootViewController.view != nil) { - onStop((__bridge CFTypeRef)self.window.rootViewController.view); + GioViewController *vc = (GioViewController *)self.window.rootViewController; + UIView *drawView = vc.view.subviews[0]; + if (drawView != nil) { + onStop((__bridge CFTypeRef)drawView); } } - (void)applicationWillEnterForeground:(UIApplication *)application { GioViewController *c = (GioViewController*)self.window.rootViewController; - if (c.view != nil) { - CFTypeRef viewRef = (__bridge CFTypeRef)c.view; + UIView *drawView = c.view.subviews[0]; + if (drawView != nil) { + CFTypeRef viewRef = (__bridge CFTypeRef)drawView; redraw(viewRef, YES); } } @@ -73,24 +76,60 @@ static void redraw(CFTypeRef viewRef, BOOL sync) { @end @implementation GioViewController +CGFloat _keyboardHeight; + - (void)loadView { CGRect zeroFrame = CGRectMake(0, 0, 0, 0); - self.view = [[GioView alloc] initWithFrame:zeroFrame]; + self.view = [[UIView alloc] initWithFrame:zeroFrame]; + UIView *drawView = [[GioView alloc] initWithFrame:zeroFrame]; + [self.view addSubview: drawView]; #ifndef TARGET_OS_TV - self.view.multipleTouchEnabled = YES; + drawView.multipleTouchEnabled = YES; #endif - self.view.contentScaleFactor = self.screen.nativeScale; - onCreate((__bridge CFTypeRef)self.view); + drawView.contentScaleFactor = self.screen.nativeScale; + drawView.preservesSuperviewLayoutMargins = YES; + drawView.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0); + onCreate((__bridge CFTypeRef)drawView); + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(keyboardWillChange:) + name:UIKeyboardWillShowNotification + object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(keyboardWillChange:) + name:UIKeyboardWillChangeFrameNotification + object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(keyboardWillHide:) + name:UIKeyboardWillHideNotification + object:nil]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; - CFTypeRef viewRef = (__bridge CFTypeRef)self.view; + CFTypeRef viewRef = (__bridge CFTypeRef)self.view.subviews[0]; onDestroy(viewRef); } - (void)viewDidLayoutSubviews { - redraw((__bridge CFTypeRef)self.view, YES); + [super viewDidLayoutSubviews]; + UIView *view = self.view.subviews[0]; + CGRect frame = self.view.bounds; + // Adjust view bounds to make room for the keyboard. + frame.size.height -= _keyboardHeight; + view.frame = frame; + redraw((__bridge CFTypeRef)view, YES); +} + +- (void)keyboardWillChange:(NSNotification *)note { + NSDictionary *userInfo = note.userInfo; + CGRect f = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; + _keyboardHeight = f.size.height; + [self.view setNeedsLayout]; +} + +- (void)keyboardWillHide:(NSNotification *)note { + _keyboardHeight = 0.0; + [self.view setNeedsLayout]; } @end