From 1359fe9e98b2978a72f0a95bf380bfc222038629 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sat, 21 Mar 2020 04:05:56 -0600 Subject: [PATCH 1/2] fix(iOS): Fix scroll issues related to keyboard dismissal (#176) (#399) - We only need the KeyboardWillHide event to fix this problem - Do some math to make sure the offset when the keyboard goes down doesn't result in empty white space being displayed. - This solution can potentially be removed when apple finally releases the fix for the issue discussed here: https://github.com/apache/cordova-ios/issues/417#issuecomment-423340885 For full details see PR #533 --- src/ios/CDVWKWebViewEngine.m | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/src/ios/CDVWKWebViewEngine.m b/src/ios/CDVWKWebViewEngine.m index fb208dd0..230b6aa2 100644 --- a/src/ios/CDVWKWebViewEngine.m +++ b/src/ios/CDVWKWebViewEngine.m @@ -114,8 +114,6 @@ @implementation CDVWKWebViewEngine @synthesize engineWebView = _engineWebView; -NSTimer *timer; - - (instancetype)initWithFrame:(CGRect)frame { self = [super init]; @@ -303,15 +301,12 @@ - (void)pluginInitialize selector:@selector(onAppWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil]; + // For keyboard dismissal leaving viewport shifted (can potentially be removed when apple releases the fix for the issue discussed here: https://github.com/apache/cordova-ios/issues/417#issuecomment-423340885) [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil]; - [[NSNotificationCenter defaultCenter] - addObserver:self - selector:@selector(keyboardWillShow) - name:UIKeyboardWillShowNotification object:nil]; NSLog(@"Using Ionic WKWebView"); @@ -378,27 +373,22 @@ - (void)onAppWillEnterForeground:(NSNotification *)notification { -(void)keyboardWillHide { - if (@available(iOS 12.0, *)) { - timer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(keyboardDisplacementFix) userInfo:nil repeats:false]; - [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; - } -} + // For keyboard dismissal leaving viewport shifted (can potentially be removed when apple releases the fix for the issue discussed here: https://github.com/apache/cordova-ios/issues/417#issuecomment-423340885) + UIScrollView * scrollView = self.webView.scrollView; + // Calculate some vars for convenience + CGFloat contentLengthWithInsets = scrollView.contentSize.height + scrollView.adjustedContentInset.top + scrollView.adjustedContentInset.bottom; + CGFloat contentOffsetY = scrollView.contentOffset.y; + CGFloat screenHeight = scrollView.frame.size.height; + CGFloat maxAllowedOffsetY = fmax(contentLengthWithInsets - screenHeight, 0); // 0 is for the case where content is shorter than screen --(void)keyboardWillShow -{ - if (timer != nil) { - [timer invalidate]; + // If the keyboard allowed the user to get to an offset beyond the max + if (contentOffsetY > maxAllowedOffsetY) { + // Reset the scroll to the max allowed so that there is no additional empty white space at the bottom where the keyboard occupied! + CGPoint bottomOfPage = CGPointMake(scrollView.contentOffset.x, maxAllowedOffsetY); + [scrollView setContentOffset:bottomOfPage]; } } --(void)keyboardDisplacementFix -{ - // https://stackoverflow.com/a/9637807/824966 - [UIView animateWithDuration:.25 animations:^{ - self.webView.scrollView.contentOffset = CGPointMake(0, 0); - }]; - -} - (BOOL)shouldReloadWebView { WKWebView* wkWebView = (WKWebView*)_engineWebView; From df0742fe8ccfc25e69bdc894d623ba7213c65cee Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Fri, 27 Mar 2020 04:42:36 -0600 Subject: [PATCH 2/2] (ios) Limit the keyboard dismissal bug fix to ios [11.0, 13.4) because in ios 13.4 Apple fixed the problem. See https://github.com/ionic-team/cordova-plugin-ionic-webview/pull/533#issuecomment-604924818 --- src/ios/CDVWKWebViewEngine.m | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/ios/CDVWKWebViewEngine.m b/src/ios/CDVWKWebViewEngine.m index 230b6aa2..a318962a 100644 --- a/src/ios/CDVWKWebViewEngine.m +++ b/src/ios/CDVWKWebViewEngine.m @@ -301,12 +301,15 @@ - (void)pluginInitialize selector:@selector(onAppWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil]; - // For keyboard dismissal leaving viewport shifted (can potentially be removed when apple releases the fix for the issue discussed here: https://github.com/apache/cordova-ios/issues/417#issuecomment-423340885) - [[NSNotificationCenter defaultCenter] - addObserver:self - selector:@selector(keyboardWillHide) - name:UIKeyboardWillHideNotification object:nil]; - + // If less than ios 13.4 + if (@available(iOS 13.4, *)) {} else { + // For keyboard dismissal leaving viewport shifted (can potentially be removed when apple releases the fix for the issue discussed here: https://github.com/apache/cordova-ios/issues/417#issuecomment-423340885) + // Apple has released a fix in 13.4, but not in 12.x (as of 12.4.6) + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(keyboardWillHide) + name:UIKeyboardWillHideNotification object:nil]; + } NSLog(@"Using Ionic WKWebView");