mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-06 01:45:36 +00:00
ui/app: (iOS) resize window when the software keyboard is shown
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+53
-14
@@ -9,14 +9,14 @@
|
|||||||
#include "os_ios.h"
|
#include "os_ios.h"
|
||||||
#include "framework_ios.h"
|
#include "framework_ios.h"
|
||||||
|
|
||||||
@interface GioViewController : UIViewController
|
|
||||||
@property UIScreen *screen;
|
|
||||||
@end
|
|
||||||
|
|
||||||
@interface GioView: UIView <UIKeyInput>
|
@interface GioView: UIView <UIKeyInput>
|
||||||
- (void)setAnimating:(BOOL)anim;
|
- (void)setAnimating:(BOOL)anim;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
@interface GioViewController : UIViewController
|
||||||
|
@property(weak) UIScreen *screen;
|
||||||
|
@end
|
||||||
|
|
||||||
static void redraw(CFTypeRef viewRef, BOOL sync) {
|
static void redraw(CFTypeRef viewRef, BOOL sync) {
|
||||||
UIView *v = (__bridge UIView *)viewRef;
|
UIView *v = (__bridge UIView *)viewRef;
|
||||||
CGFloat scale = v.layer.contentsScale;
|
CGFloat scale = v.layer.contentsScale;
|
||||||
@@ -48,15 +48,18 @@ static void redraw(CFTypeRef viewRef, BOOL sync) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (void)applicationDidEnterBackground:(UIApplication *)application {
|
- (void)applicationDidEnterBackground:(UIApplication *)application {
|
||||||
if (self.window.rootViewController.view != nil) {
|
GioViewController *vc = (GioViewController *)self.window.rootViewController;
|
||||||
onStop((__bridge CFTypeRef)self.window.rootViewController.view);
|
UIView *drawView = vc.view.subviews[0];
|
||||||
|
if (drawView != nil) {
|
||||||
|
onStop((__bridge CFTypeRef)drawView);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)applicationWillEnterForeground:(UIApplication *)application {
|
- (void)applicationWillEnterForeground:(UIApplication *)application {
|
||||||
GioViewController *c = (GioViewController*)self.window.rootViewController;
|
GioViewController *c = (GioViewController*)self.window.rootViewController;
|
||||||
if (c.view != nil) {
|
UIView *drawView = c.view.subviews[0];
|
||||||
CFTypeRef viewRef = (__bridge CFTypeRef)c.view;
|
if (drawView != nil) {
|
||||||
|
CFTypeRef viewRef = (__bridge CFTypeRef)drawView;
|
||||||
redraw(viewRef, YES);
|
redraw(viewRef, YES);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -73,24 +76,60 @@ static void redraw(CFTypeRef viewRef, BOOL sync) {
|
|||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation GioViewController
|
@implementation GioViewController
|
||||||
|
CGFloat _keyboardHeight;
|
||||||
|
|
||||||
- (void)loadView {
|
- (void)loadView {
|
||||||
CGRect zeroFrame = CGRectMake(0, 0, 0, 0);
|
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
|
#ifndef TARGET_OS_TV
|
||||||
self.view.multipleTouchEnabled = YES;
|
drawView.multipleTouchEnabled = YES;
|
||||||
#endif
|
#endif
|
||||||
self.view.contentScaleFactor = self.screen.nativeScale;
|
drawView.contentScaleFactor = self.screen.nativeScale;
|
||||||
onCreate((__bridge CFTypeRef)self.view);
|
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 {
|
- (void)viewDidDisappear:(BOOL)animated {
|
||||||
[super viewDidDisappear:animated];
|
[super viewDidDisappear:animated];
|
||||||
CFTypeRef viewRef = (__bridge CFTypeRef)self.view;
|
CFTypeRef viewRef = (__bridge CFTypeRef)self.view.subviews[0];
|
||||||
onDestroy(viewRef);
|
onDestroy(viewRef);
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)viewDidLayoutSubviews {
|
- (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
|
@end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user