Selaa lähdekoodia

Reapplied #134 with additional checks to tackle unintentional scrolls

Alexander Stammbach 6 vuotta sitten
vanhempi
commit
efb7de36a2
1 muutettua tiedostoa jossa 39 lisäystä ja 0 poistoa
  1. 39
    0
      ios/RNCWKWebView.m

+ 39
- 0
ios/RNCWKWebView.m Näytä tiedosto

13
 
13
 
14
 #import "objc/runtime.h"
14
 #import "objc/runtime.h"
15
 
15
 
16
+static NSTimer *keyboardTimer;
16
 static NSString *const MessageHanderName = @"ReactNative";
17
 static NSString *const MessageHanderName = @"ReactNative";
17
 
18
 
18
 // runtime trick to remove WKWebView keyboard default toolbar
19
 // runtime trick to remove WKWebView keyboard default toolbar
70
     _automaticallyAdjustContentInsets = YES;
71
     _automaticallyAdjustContentInsets = YES;
71
     _contentInset = UIEdgeInsetsZero;
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
   return self;
88
   return self;
74
 }
89
 }
75
 
90
 
146
     [super removeFromSuperview];
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
 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
188
 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
150
     if ([keyPath isEqual:@"estimatedProgress"] && object == self.webView) {
189
     if ([keyPath isEqual:@"estimatedProgress"] && object == self.webView) {
151
         if(_onLoadingProgress){
190
         if(_onLoadingProgress){