Skip to content

Commit 09f9e8e

Browse files
Lindsay-Needs-SleepNatiSP27
authored andcommitted
fix(ios): avoid app scrolling to top on keyboard hide (ionic-team#533)
(cherry picked from commit 7974eb4) (cherry picked from commit af642d8)
1 parent d93f4e4 commit 09f9e8e

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed

src/ios/CDVWKWebViewEngine.m

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ @implementation CDVWKWebViewEngine
114114

115115
@synthesize engineWebView = _engineWebView;
116116

117-
NSTimer *timer;
118-
119117
- (instancetype)initWithFrame:(CGRect)frame
120118
{
121119
self = [super init];
@@ -303,15 +301,15 @@ - (void)pluginInitialize
303301
selector:@selector(onAppWillEnterForeground:)
304302
name:UIApplicationWillEnterForegroundNotification object:nil];
305303

306-
[[NSNotificationCenter defaultCenter]
307-
addObserver:self
308-
selector:@selector(keyboardWillHide)
309-
name:UIKeyboardWillHideNotification object:nil];
310-
311-
[[NSNotificationCenter defaultCenter]
312-
addObserver:self
313-
selector:@selector(keyboardWillShow)
314-
name:UIKeyboardWillShowNotification object:nil];
304+
// If less than ios 13.4
305+
if (@available(iOS 13.4, *)) {} else {
306+
// 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)
307+
// Apple has released a fix in 13.4, but not in 12.x (as of 12.4.6)
308+
[[NSNotificationCenter defaultCenter]
309+
addObserver:self
310+
selector:@selector(keyboardWillHide)
311+
name:UIKeyboardWillHideNotification object:nil];
312+
}
315313

316314
NSLog(@"Using Ionic WKWebView");
317315

@@ -378,27 +376,22 @@ - (void)onAppWillEnterForeground:(NSNotification *)notification {
378376

379377
-(void)keyboardWillHide
380378
{
381-
if (@available(iOS 12.0, *)) {
382-
timer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(keyboardDisplacementFix) userInfo:nil repeats:false];
383-
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
384-
}
385-
}
379+
// 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)
380+
UIScrollView * scrollView = self.webView.scrollView;
381+
// Calculate some vars for convenience
382+
CGFloat contentLengthWithInsets = scrollView.contentSize.height + scrollView.adjustedContentInset.top + scrollView.adjustedContentInset.bottom;
383+
CGFloat contentOffsetY = scrollView.contentOffset.y;
384+
CGFloat screenHeight = scrollView.frame.size.height;
385+
CGFloat maxAllowedOffsetY = fmax(contentLengthWithInsets - screenHeight, 0); // 0 is for the case where content is shorter than screen
386386

387-
-(void)keyboardWillShow
388-
{
389-
if (timer != nil) {
390-
[timer invalidate];
387+
// If the keyboard allowed the user to get to an offset beyond the max
388+
if (contentOffsetY > maxAllowedOffsetY) {
389+
// Reset the scroll to the max allowed so that there is no additional empty white space at the bottom where the keyboard occupied!
390+
CGPoint bottomOfPage = CGPointMake(scrollView.contentOffset.x, maxAllowedOffsetY);
391+
[scrollView setContentOffset:bottomOfPage];
391392
}
392393
}
393394

394-
-(void)keyboardDisplacementFix
395-
{
396-
// https://stackoverflow.com/a/9637807/824966
397-
[UIView animateWithDuration:.25 animations:^{
398-
self.webView.scrollView.contentOffset = CGPointMake(0, 0);
399-
}];
400-
401-
}
402395
- (BOOL)shouldReloadWebView
403396
{
404397
WKWebView* wkWebView = (WKWebView*)_engineWebView;

0 commit comments

Comments
 (0)