From efb7de36a211dbb9ad032c5d41ca9add3042cff3 Mon Sep 17 00:00:00 2001 From: Alexander Stammbach Date: Thu, 31 Jan 2019 18:00:17 +0100 Subject: [PATCH] Reapplied #134 with additional checks to tackle unintentional scrolls --- ios/RNCWKWebView.m | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/ios/RNCWKWebView.m b/ios/RNCWKWebView.m index fee82a8..3d6da98 100644 --- a/ios/RNCWKWebView.m +++ b/ios/RNCWKWebView.m @@ -13,6 +13,7 @@ #import "objc/runtime.h" +static NSTimer *keyboardTimer; static NSString *const MessageHanderName = @"ReactNative"; // runtime trick to remove WKWebView keyboard default toolbar @@ -70,6 +71,20 @@ static NSString *const MessageHanderName = @"ReactNative"; _automaticallyAdjustContentInsets = YES; _contentInset = UIEdgeInsetsZero; } + + // Workaround for a keyboard dismissal bug present in iOS 12 + // https://openradar.appspot.com/radar?id=5018321736957952 + if (@available(iOS 12.0, *)) { + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(keyboardWillHide) + name:UIKeyboardWillHideNotification object:nil]; + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(keyboardWillShow) + name:UIKeyboardWillShowNotification object:nil]; + } + return self; } @@ -146,6 +161,30 @@ static NSString *const MessageHanderName = @"ReactNative"; [super removeFromSuperview]; } +-(void)keyboardWillHide +{ + keyboardTimer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(keyboardDisplacementFix) userInfo:nil repeats:false]; + [[NSRunLoop mainRunLoop] addTimer:keyboardTimer forMode:NSRunLoopCommonModes]; +} +-(void)keyboardWillShow +{ + if (keyboardTimer != nil) { + [keyboardTimer invalidate]; + } +} +-(void)keyboardDisplacementFix +{ + // Additional viewport checks to prevent unintentional scrolls + UIScrollView *scrollView = self.webView.scrollView; + double maxContentOffset = scrollView.contentSize.height - scrollView.frame.size.height; + if(scrollView.contentOffset.y > maxContentOffset) { + // https://stackoverflow.com/a/9637807/824966 + [UIView animateWithDuration:.25 animations:^{ + self.webView.scrollView.contentOffset = CGPointMake(0, maxContentOffset); + }]; + } +} + - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ if ([keyPath isEqual:@"estimatedProgress"] && object == self.webView) { if(_onLoadingProgress){