Browse Source

feat(iOS): added `contentInsetAdjustmentBehavior` prop

* Adding a `contentInsetAdjustmentBehavior` prop to the WebView for iOS.
This controls the way iOS will automatically adjust the insets when the
webview is behind things like the iPhone X notch.

* Removing the code to explicitly pass contentInsetAdjustmentBehavior to the WebView since it is already passed in otherProps.
aarondail 4 years ago
parent
commit
493b65de21
4 changed files with 68 additions and 1 deletions
  1. 18
    0
      docs/Reference.md
  2. 25
    1
      ios/RNCWKWebView.m
  3. 17
    0
      ios/RNCWKWebViewManager.m
  4. 8
    0
      src/WebViewTypes.ts

+ 18
- 0
docs/Reference.md View File

@@ -36,6 +36,7 @@ This document lays out the current public properties and methods for the React N
36 36
 - [`bounces`](Reference.md#bounces)
37 37
 - [`overScrollMode`](Reference.md#overscrollmode)
38 38
 - [`contentInset`](Reference.md#contentinset)
39
+- [`contentInsetAdjustmentBehavior`](Reference.md#contentInsetAdjustmentBehavior)
39 40
 - [`dataDetectorTypes`](Reference.md#datadetectortypes)
40 41
 - [`scrollEnabled`](Reference.md#scrollenabled)
41 42
 - [`directionalLockEnabled`](Reference.md#directionalLockEnabled)
@@ -689,6 +690,23 @@ The amount by which the web view content is inset from the edges of the scroll v
689 690
 
690 691
 ---
691 692
 
693
+### `contentInsetAdjustmentBehavior`
694
+
695
+This property specifies how the safe area insets are used to modify the content area of the scroll view. The default value of this property is "never". Available on iOS 11 and later. Defaults to `never`.
696
+
697
+Possible values:
698
+
699
+- `automatic`
700
+- `scrollableAxes`
701
+- `never`
702
+- `always`
703
+
704
+| Type   | Required | Platform |
705
+| ------ | -------- | -------- |
706
+| string | No       | iOS      |
707
+
708
+---
709
+
692 710
 ### `dataDetectorTypes`
693 711
 
694 712
 Determines the types of data converted to clickable URLs in the web view's content. By default only phone numbers are detected.

+ 25
- 1
ios/RNCWKWebView.m View File

@@ -49,6 +49,10 @@ static NSURLCredential* clientAuthenticationCredential;
49 49
   BOOL _isFullScreenVideoOpen;
50 50
   UIStatusBarStyle _savedStatusBarStyle;
51 51
   BOOL _savedStatusBarHidden;
52
+
53
+#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
54
+  UIScrollViewContentInsetAdjustmentBehavior _savedContentInsetAdjustmentBehavior;
55
+#endif
52 56
 }
53 57
 
54 58
 - (instancetype)initWithFrame:(CGRect)frame
@@ -65,6 +69,10 @@ static NSURLCredential* clientAuthenticationCredential;
65 69
     _savedKeyboardDisplayRequiresUserAction = YES;
66 70
     _savedStatusBarStyle = RCTSharedApplication().statusBarStyle;
67 71
     _savedStatusBarHidden = RCTSharedApplication().statusBarHidden;
72
+
73
+#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
74
+    _savedContentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
75
+#endif
68 76
   }
69 77
 
70 78
   if (@available(iOS 12.0, *)) {
@@ -227,7 +235,7 @@ static NSURLCredential* clientAuthenticationCredential;
227 235
     }
228 236
 #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
229 237
     if ([_webView.scrollView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
230
-      _webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
238
+      _webView.scrollView.contentInsetAdjustmentBehavior = _savedContentInsetAdjustmentBehavior;
231 239
     }
232 240
 #endif
233 241
 
@@ -328,6 +336,22 @@ static NSURLCredential* clientAuthenticationCredential;
328 336
   _webView.backgroundColor = backgroundColor;
329 337
 }
330 338
 
339
+#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
340
+- (void)setContentInsetAdjustmentBehavior:(UIScrollViewContentInsetAdjustmentBehavior)behavior
341
+{
342
+    _savedContentInsetAdjustmentBehavior = behavior;
343
+    if (_webView == nil) {
344
+        return;
345
+    }
346
+
347
+    if ([_webView.scrollView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
348
+        CGPoint contentOffset = _webView.scrollView.contentOffset;
349
+        _webView.scrollView.contentInsetAdjustmentBehavior = behavior;
350
+        _webView.scrollView.contentOffset = contentOffset;
351
+    }
352
+}
353
+#endif
354
+
331 355
 /**
332 356
  * This method is called whenever JavaScript running within the web view calls:
333 357
  *   - window.webkit.messageHandlers[MessageHandlerName].postMessage

+ 17
- 0
ios/RNCWKWebViewManager.m View File

@@ -14,6 +14,19 @@
14 14
 @interface RNCWKWebViewManager () <RNCWKWebViewDelegate>
15 15
 @end
16 16
 
17
+@implementation RCTConvert (UIScrollView)
18
+
19
+#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
20
+RCT_ENUM_CONVERTER(UIScrollViewContentInsetAdjustmentBehavior, (@{
21
+                                                                  @"automatic": @(UIScrollViewContentInsetAdjustmentAutomatic),
22
+                                                                  @"scrollableAxes": @(UIScrollViewContentInsetAdjustmentScrollableAxes),
23
+                                                                  @"never": @(UIScrollViewContentInsetAdjustmentNever),
24
+                                                                  @"always": @(UIScrollViewContentInsetAdjustmentAlways),
25
+                                                                  }), UIScrollViewContentInsetAdjustmentNever, integerValue)
26
+#endif
27
+
28
+@end
29
+
17 30
 @implementation RNCWKWebViewManager
18 31
 {
19 32
   NSConditionLock *_shouldStartLoadLock;
@@ -52,6 +65,10 @@ RCT_EXPORT_VIEW_PROPERTY(applicationNameForUserAgent, NSString)
52 65
 RCT_EXPORT_VIEW_PROPERTY(cacheEnabled, BOOL)
53 66
 RCT_EXPORT_VIEW_PROPERTY(allowsLinkPreview, BOOL)
54 67
 
68
+#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
69
+RCT_EXPORT_VIEW_PROPERTY(contentInsetAdjustmentBehavior, UIScrollViewContentInsetAdjustmentBehavior)
70
+#endif
71
+
55 72
 /**
56 73
  * Expose methods to enable messaging the webview.
57 74
  */

+ 8
- 0
src/WebViewTypes.ts View File

@@ -260,6 +260,7 @@ export interface IOSNativeWebViewProps extends CommonNativeWebViewProps {
260 260
   automaticallyAdjustContentInsets?: boolean;
261 261
   bounces?: boolean;
262 262
   contentInset?: ContentInsetProp;
263
+  contentInsetAdjustmentBehavior?: 'automatic'| 'scrollableAxes' | 'never' | 'always';
263 264
   dataDetectorTypes?: DataDetectorTypes | ReadonlyArray<DataDetectorTypes>;
264 265
   decelerationRate?: number;
265 266
   directionalLockEnabled?: boolean;
@@ -324,6 +325,13 @@ export interface IOSWebViewProps extends WebViewSharedProps {
324 325
    */
325 326
   automaticallyAdjustContentInsets?: boolean;
326 327
 
328
+  /**
329
+   * This property specifies how the safe area insets are used to modify the
330
+   * content area of the scroll view. The default value of this property is
331
+   * "never". Available on iOS 11 and later.
332
+   */
333
+  contentInsetAdjustmentBehavior?: 'automatic'| 'scrollableAxes' | 'never' | 'always'
334
+
327 335
   /**
328 336
    * The amount by which the web view content is inset from the edges of
329 337
    * the scroll view. Defaults to {top: 0, left: 0, bottom: 0, right: 0}.