Parcourir la source

fix(WKWebView.m): Reapplied #134 with additional checks to tackle unintentional scrolls (#296)

Alexander Stammbach il y a 5 ans
Parent
révision
3f58b0e597
1 fichiers modifiés avec 42 ajouts et 0 suppressions
  1. 42
    0
      ios/RNCWKWebView.m

+ 42
- 0
ios/RNCWKWebView.m Voir le fichier

@@ -13,6 +13,7 @@
13 13
 
14 14
 #import "objc/runtime.h"
15 15
 
16
+static NSTimer *keyboardTimer;
16 17
 static NSString *const MessageHandlerName = @"ReactNativeWebView";
17 18
 
18 19
 // runtime trick to remove WKWebView keyboard default toolbar
@@ -70,6 +71,20 @@ static NSString *const MessageHandlerName = @"ReactNativeWebView";
70 71
     _automaticallyAdjustContentInsets = YES;
71 72
     _contentInset = UIEdgeInsetsZero;
72 73
   }
74
+
75
+  // Workaround for a keyboard dismissal bug present in iOS 12
76
+  // https://openradar.appspot.com/radar?id=5018321736957952
77
+  if (@available(iOS 12.0, *)) {
78
+    [[NSNotificationCenter defaultCenter]
79
+      addObserver:self
80
+      selector:@selector(keyboardWillHide)
81
+      name:UIKeyboardWillHideNotification object:nil];
82
+    [[NSNotificationCenter defaultCenter]
83
+      addObserver:self
84
+      selector:@selector(keyboardWillShow)
85
+      name:UIKeyboardWillShowNotification object:nil];
86
+  }
87
+
73 88
   return self;
74 89
 }
75 90
 
@@ -172,6 +187,33 @@ static NSString *const MessageHandlerName = @"ReactNativeWebView";
172 187
     [super removeFromSuperview];
173 188
 }
174 189
 
190
+-(void)keyboardWillHide
191
+{
192
+    keyboardTimer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(keyboardDisplacementFix) userInfo:nil repeats:false];
193
+    [[NSRunLoop mainRunLoop] addTimer:keyboardTimer forMode:NSRunLoopCommonModes];
194
+}
195
+-(void)keyboardWillShow
196
+{
197
+    if (keyboardTimer != nil) {
198
+        [keyboardTimer invalidate];
199
+    }
200
+}
201
+-(void)keyboardDisplacementFix
202
+{
203
+    // Additional viewport checks to prevent unintentional scrolls
204
+    UIScrollView *scrollView = self.webView.scrollView;
205
+    double maxContentOffset = scrollView.contentSize.height - scrollView.frame.size.height;
206
+    if (maxContentOffset < 0) {
207
+        maxContentOffset = 0;
208
+    }
209
+    if (scrollView.contentOffset.y > maxContentOffset) {
210
+      // https://stackoverflow.com/a/9637807/824966
211
+      [UIView animateWithDuration:.25 animations:^{
212
+          scrollView.contentOffset = CGPointMake(0, maxContentOffset);
213
+      }];
214
+    }
215
+}
216
+
175 217
 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
176 218
     if ([keyPath isEqual:@"estimatedProgress"] && object == self.webView) {
177 219
         if(_onLoadingProgress){