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

Fix ios 12 black area when keyboard is showing and shrink param is true #1

Merged
merged 1 commit into from
Jan 28, 2019
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Shrink the WebView when the keyboard comes up.

Set to true to shrink the WebView when the keyboard comes up. The WebView shrinks instead of the viewport shrinking and the page scrollable. This applies to apps that position their elements relative to the bottom of the WebView. This is the default behaviour on Android, and makes a lot of sense when building apps as opposed to webpages.

**Fixed scroll the content to bottom when keyboard is showing and the shrinkview param is true. Work with meta viewport-fit=cover fix for iphone x.**

#### Supported Platforms

Expand Down
32 changes: 26 additions & 6 deletions src/ios/CDVKeyboard.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Licensed to the Apache Software Foundation (ASF) under one
@interface CDVKeyboard () <UIScrollViewDelegate>

@property (nonatomic, readwrite, assign) BOOL keyboardIsVisible;
@property (readwrite, assign) CGFloat keyboardHeight;

@end

Expand Down Expand Up @@ -193,10 +194,14 @@ - (void)shrinkViewKeyboardWillChangeFrame:(NSNotification*)notif
// Note: we check for _shrinkView at this point instead of the beginning of the method to handle
// the case where the user disabled shrinkView while the keyboard is showing.
// The webview should always be able to return to full size
_shrinkView = YES;
CGRect keyboardIntersection = CGRectIntersection(screen, keyboard);
if (CGRectContainsRect(screen, keyboardIntersection) && !CGRectIsEmpty(keyboardIntersection) && _shrinkView && self.keyboardIsVisible) {
screen.size.height -= keyboardIntersection.size.height;
self.webView.scrollView.scrollEnabled = !self.disableScrollingInShrinkView;
self.keyboardHeight = keyboardIntersection.size.height;

// self.webView.scrollView.scrollEnabled = !self.disableScrollingInShrinkView;
self.webView.scrollView.scrollEnabled = NO;
}

// A view's frame is in its superview's coordinate system so we need to convert again
Expand All @@ -207,12 +212,24 @@ - (void)shrinkViewKeyboardWillChangeFrame:(NSNotification*)notif

- (void)scrollViewDidScroll:(UIScrollView*)scrollView
{
/*
Costin Moraru

On ios 12 with default cordova webview won't scroll webview content to bottom with bounds.
Used setContentOffset to scroll webview content to 0.0f.

*/
// if (_shrinkView && _keyboardIsVisible) {
// CGFloat maxY = scrollView.contentSize.height - scrollView.bounds.size.height;
// if (scrollView.bounds.origin.y > maxY) {
// scrollView.bounds = CGRectMake(scrollView.bounds.origin.x, maxY,
// scrollView.bounds.size.width, scrollView.bounds.size.height);
// }
// }
if (_shrinkView && _keyboardIsVisible) {
CGFloat maxY = scrollView.contentSize.height - scrollView.bounds.size.height;
if (scrollView.bounds.origin.y > maxY) {
scrollView.bounds = CGRectMake(scrollView.bounds.origin.x, maxY,
scrollView.bounds.size.width, scrollView.bounds.size.height);
}
// Scroll webview content to bottom
CGPoint bottomOffset = CGPointMake(0.0f, 0.0f);
[self.webView.scrollView setContentOffset:bottomOffset animated:NO];
}
}

Expand All @@ -227,6 +244,9 @@ - (void)shrinkView:(CDVInvokedUrlCommand*)command
}

self.shrinkView = [value boolValue];
// Scroll webview content to bottom
CGPoint bottomOffset = CGPointMake(0.0f, 0.0f);
[self.webView.scrollView setContentOffset:bottomOffset animated:NO];
}

[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:self.shrinkView]
Expand Down