Kaynağa Gözat

Reapplied #134 with additional checks to tackle unintentional scrolls

Alexander Stammbach 6 yıl önce
ebeveyn
işleme
efb7de36a2
1 değiştirilmiş dosya ile 39 ekleme ve 0 silme
  1. 39
    0
      ios/RNCWKWebView.m

+ 39
- 0
ios/RNCWKWebView.m Dosyayı Görüntüle

@@ -13,6 +13,7 @@
13 13
 
14 14
 #import "objc/runtime.h"
15 15
 
16
+static NSTimer *keyboardTimer;
16 17
 static NSString *const MessageHanderName = @"ReactNative";
17 18
 
18 19
 // runtime trick to remove WKWebView keyboard default toolbar
@@ -70,6 +71,20 @@ static NSString *const MessageHanderName = @"ReactNative";
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
 
@@ -146,6 +161,30 @@ static NSString *const MessageHanderName = @"ReactNative";
146 161
     [super removeFromSuperview];
147 162
 }
148 163
 
164
+-(void)keyboardWillHide
165
+{
166
+    keyboardTimer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(keyboardDisplacementFix) userInfo:nil repeats:false];
167
+    [[NSRunLoop mainRunLoop] addTimer:keyboardTimer forMode:NSRunLoopCommonModes];
168
+}
169
+-(void)keyboardWillShow
170
+{
171
+    if (keyboardTimer != nil) {
172
+        [keyboardTimer invalidate];
173
+    }
174
+}
175
+-(void)keyboardDisplacementFix
176
+{
177
+    // Additional viewport checks to prevent unintentional scrolls
178
+    UIScrollView *scrollView = self.webView.scrollView;
179
+    double maxContentOffset = scrollView.contentSize.height - scrollView.frame.size.height;
180
+    if(scrollView.contentOffset.y > maxContentOffset) {
181
+      // https://stackoverflow.com/a/9637807/824966
182
+      [UIView animateWithDuration:.25 animations:^{
183
+          self.webView.scrollView.contentOffset = CGPointMake(0, maxContentOffset);
184
+      }];
185
+    }
186
+}
187
+
149 188
 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
150 189
     if ([keyPath isEqual:@"estimatedProgress"] && object == self.webView) {
151 190
         if(_onLoadingProgress){