Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement view resizing on keyboard show/hide #1119

Merged
merged 4 commits into from
Jan 23, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions ios/Capacitor/Capacitor/Plugins/Keyboard.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,38 @@ @implementation CAPKeyboard
- (void)load
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarDidChangeFrame:) name:UIApplicationDidChangeStatusBarFrameNotification object: nil];

NSString * style = [self getConfigValue:@"style"];
if ([style isEqualToString:@"dark"]) {
[self setKeyboardAppearanceDark];
}

self.keyboardResizes = ResizeNative;

BOOL doesResize = YES;
if (!doesResize) {
if ([[self getConfigValue:@"resize"] isEqualToString:@"none"]) {
doesResize = NO;
self.keyboardResizes = ResizeNone;
NSLog(@"CAPIonicKeyboard: no resize");
NSLog(@"CAPKeyboard: no resize");
}

if (doesResize) {
self.keyboardResizes = ResizeNative;
NSString * resizeMode = [self getConfigValue:@"resize"];

} else {
NSString *resizeMode = @"ionic";
if (resizeMode) {
if ([resizeMode isEqualToString:@"ionic"]) {
self.keyboardResizes = ResizeIonic;
NSLog(@"CAPKeyboard: resize mode - ionic");
} else if ([resizeMode isEqualToString:@"body"]) {
self.keyboardResizes = ResizeBody;
NSLog(@"CAPKeyboard: resize mode - body");
}
}
// NSLog(@"CAPIonicKeyboard: resize mode %d", self.keyboardResizes);

if (self.keyboardResizes == ResizeNative) {
NSLog(@"CAPKeyboard: resize mode - native");
}
}

self.hideFormAccessoryBar = YES;

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
Expand Down Expand Up @@ -152,6 +162,16 @@ - (void)setPaddingBottom:(int)paddingBottom delay:(NSTimeInterval)delay
}
}

- (void)resizeElement:(NSString *)element withPaddingBottom:(int)paddingBottom withScreenHeight:(int)screenHeight
{
int height = -1;
if (paddingBottom > 0) {
height = screenHeight - paddingBottom;
}

[self.bridge evalWithJs: [NSString stringWithFormat:@"(function() { var el = %@; var height = %d; if (el) { el.style.height = height > -1 ? height + 'px' : null; } })()", element, height]];
}

- (void)_updateFrame
{
CGSize statusBarSize = [[UIApplication sharedApplication] statusBarFrame].size;
Expand All @@ -167,16 +187,12 @@ - (void)_updateFrame
switch (self.keyboardResizes) {
case ResizeBody:
{
NSString *js = [NSString stringWithFormat:@"plugin.fireOnResize(%d, %d, document.body);",
_paddingBottom, (int)f.size.height];
[self.bridge evalWithPlugin:self js:js];
[self resizeElement:@"document.body" withPaddingBottom:_paddingBottom withScreenHeight:(int)f.size.height];
break;
}
case ResizeIonic:
{
NSString *js = [NSString stringWithFormat:@"plugin.fireOnResize(%d, %d, document.querySelector('ion-app'));",
_paddingBottom, (int)f.size.height];
[self.bridge evalWithPlugin:self js:js];
[self resizeElement:@"document.querySelector('ion-app')" withPaddingBottom:_paddingBottom withScreenHeight:(int)f.size.height];
break;
}
case ResizeNative:
Expand Down