Skip to content

Commit 08c9c3f

Browse files
fix(iOS): Fix scroll issues related to keyboard dismissal (#176) (#399)
- We only need the KeyboardWillHide event to fix this problem, and we only need it for ios 11+ - Enable the fix for ios 11+, not only ios 12+. It was probably only 12+ because no devices have a max OS of 11, so no dev probably confirmed the problem's existence (or non-existence) on 11, myself included. (But the functionality that started this issue chain began in ios 11.) - 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: apache/cordova-ios#417 (comment)
1 parent 8b2586e commit 08c9c3f

File tree

1 file changed

+22
-32
lines changed

1 file changed

+22
-32
lines changed

src/ios/CDVWKWebViewEngine.m

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,6 @@ @implementation CDVWKWebViewEngine
127127

128128
@synthesize engineWebView = _engineWebView;
129129

130-
NSTimer *timer;
131-
132130
- (instancetype)initWithFrame:(CGRect)frame
133131
{
134132
self = [super init];
@@ -410,16 +408,13 @@ - (void)pluginInitialize
410408
selector:@selector(onSocketError:)
411409
name:@"socketInUseError" object:nil];
412410

413-
[[NSNotificationCenter defaultCenter]
414-
addObserver:self
415-
selector:@selector(keyboardWillHide)
416-
name:UIKeyboardWillHideNotification object:nil];
417-
418-
[[NSNotificationCenter defaultCenter]
419-
addObserver:self
420-
selector:@selector(keyboardWillShow)
421-
name:UIKeyboardWillShowNotification object:nil];
422-
411+
if (@available(iOS 11.0, *)) {
412+
// 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)
413+
[[NSNotificationCenter defaultCenter]
414+
addObserver:self
415+
selector:@selector(keyboardWillHide)
416+
name:UIKeyboardWillHideNotification object:nil];
417+
}
423418

424419
NSLog(@"Using Ionic WKWebView");
425420

@@ -531,31 +526,26 @@ - (void)onAppWillEnterForeground:(NSNotification *)notification {
531526
}
532527
}
533528

534-
535529
-(void)keyboardWillHide
536530
{
537-
if (@available(iOS 12.0, *)) {
538-
timer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(keyboardDisplacementFix) userInfo:nil repeats:false];
539-
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
540-
}
541-
}
542-
543-
-(void)keyboardWillShow
544-
{
545-
if (timer != nil) {
546-
[timer invalidate];
531+
if (@available(iOS 11.0, *)) {
532+
// 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)
533+
UIScrollView * scrollView = self.webView.scrollView;
534+
// Calculate some vars for convenience
535+
CGFloat contentLengthWithInsets = scrollView.contentSize.height + scrollView.adjustedContentInset.top + scrollView.adjustedContentInset.bottom;
536+
CGFloat contentOffsetY = scrollView.contentOffset.y;
537+
CGFloat screenHeight = scrollView.frame.size.height;
538+
CGFloat maxAllowedOffsetY = fmax(contentLengthWithInsets - screenHeight, 0); // 0 is for the case where content is shorter than screen
539+
540+
// If the keyboard allowed the user to get to an offset beyond the max
541+
if (contentOffsetY > maxAllowedOffsetY) {
542+
// Reset the scroll to the max allowed so that there is no additional empty white space at the bottom where the keyboard occupied!
543+
CGPoint bottomOfPage = CGPointMake(scrollView.contentOffset.x, maxAllowedOffsetY);
544+
[scrollView setContentOffset:bottomOfPage];
545+
}
547546
}
548547
}
549548

550-
-(void)keyboardDisplacementFix
551-
{
552-
// https://stackoverflow.com/a/9637807/824966
553-
[UIView animateWithDuration:.25 animations:^{
554-
self.webView.scrollView.contentOffset = CGPointMake(0, 0);
555-
}];
556-
557-
}
558-
559549
- (void)onSocketError:(NSNotification *)notification {
560550
[self loadErrorPage:nil];
561551
}

0 commit comments

Comments
 (0)