Browse Source

feat(WKWebview): Add fix for keyboard dismiss leaving viewport shifted in iOS 12 (#134)

Tom 6 years ago
parent
commit
5425039ff4
1 changed files with 33 additions and 0 deletions
  1. 33
    0
      ios/RNCWKWebView.m

+ 33
- 0
ios/RNCWKWebView.m View File

@@ -11,6 +11,7 @@
11 11
 
12 12
 #import "objc/runtime.h"
13 13
 
14
+static NSTimer *keyboardTimer;
14 15
 static NSString *const MessageHanderName = @"ReactNative";
15 16
 
16 17
 // runtime trick to remove WKWebView keyboard default toolbar
@@ -74,6 +75,19 @@ static NSString *const MessageHanderName = @"ReactNative";
74 75
     _automaticallyAdjustContentInsets = YES;
75 76
     _contentInset = UIEdgeInsetsZero;
76 77
   }
78
+    
79
+  // Workaround for a keyboard dismissal bug present in iOS 12
80
+  // https://openradar.appspot.com/radar?id=5018321736957952
81
+  if (@available(iOS 12.0, *)) {
82
+    [[NSNotificationCenter defaultCenter]
83
+      addObserver:self
84
+      selector:@selector(keyboardWillHide)
85
+      name:UIKeyboardWillHideNotification object:nil];
86
+    [[NSNotificationCenter defaultCenter]
87
+      addObserver:self
88
+      selector:@selector(keyboardWillShow)
89
+      name:UIKeyboardWillShowNotification object:nil];
90
+  }
77 91
   return self;
78 92
 }
79 93
 
@@ -122,6 +136,25 @@ static NSString *const MessageHanderName = @"ReactNative";
122 136
   }
123 137
 }
124 138
 
139
+-(void)keyboardWillHide
140
+{
141
+    keyboardTimer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(keyboardDisplacementFix) userInfo:nil repeats:false];
142
+    [[NSRunLoop mainRunLoop] addTimer:keyboardTimer forMode:NSRunLoopCommonModes];
143
+}
144
+-(void)keyboardWillShow
145
+{
146
+    if (keyboardTimer != nil) {
147
+        [keyboardTimer invalidate];
148
+    }
149
+}
150
+-(void)keyboardDisplacementFix
151
+{
152
+    // https://stackoverflow.com/a/9637807/824966
153
+    [UIView animateWithDuration:.25 animations:^{
154
+        self.webView.scrollView.contentOffset = CGPointMake(0, 0);
155
+    }];
156
+}
157
+
125 158
 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
126 159
     if ([keyPath isEqual:@"estimatedProgress"] && object == self.webView) {
127 160
         if(_onLoadingProgress){