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 5 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
 - [`bounces`](Reference.md#bounces)
36
 - [`bounces`](Reference.md#bounces)
37
 - [`overScrollMode`](Reference.md#overscrollmode)
37
 - [`overScrollMode`](Reference.md#overscrollmode)
38
 - [`contentInset`](Reference.md#contentinset)
38
 - [`contentInset`](Reference.md#contentinset)
39
+- [`contentInsetAdjustmentBehavior`](Reference.md#contentInsetAdjustmentBehavior)
39
 - [`dataDetectorTypes`](Reference.md#datadetectortypes)
40
 - [`dataDetectorTypes`](Reference.md#datadetectortypes)
40
 - [`scrollEnabled`](Reference.md#scrollenabled)
41
 - [`scrollEnabled`](Reference.md#scrollenabled)
41
 - [`directionalLockEnabled`](Reference.md#directionalLockEnabled)
42
 - [`directionalLockEnabled`](Reference.md#directionalLockEnabled)
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
 ### `dataDetectorTypes`
710
 ### `dataDetectorTypes`
693
 
711
 
694
 Determines the types of data converted to clickable URLs in the web view's content. By default only phone numbers are detected.
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
   BOOL _isFullScreenVideoOpen;
49
   BOOL _isFullScreenVideoOpen;
50
   UIStatusBarStyle _savedStatusBarStyle;
50
   UIStatusBarStyle _savedStatusBarStyle;
51
   BOOL _savedStatusBarHidden;
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
 - (instancetype)initWithFrame:(CGRect)frame
58
 - (instancetype)initWithFrame:(CGRect)frame
65
     _savedKeyboardDisplayRequiresUserAction = YES;
69
     _savedKeyboardDisplayRequiresUserAction = YES;
66
     _savedStatusBarStyle = RCTSharedApplication().statusBarStyle;
70
     _savedStatusBarStyle = RCTSharedApplication().statusBarStyle;
67
     _savedStatusBarHidden = RCTSharedApplication().statusBarHidden;
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
   if (@available(iOS 12.0, *)) {
78
   if (@available(iOS 12.0, *)) {
227
     }
235
     }
228
 #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
236
 #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
229
     if ([_webView.scrollView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
237
     if ([_webView.scrollView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
230
-      _webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
238
+      _webView.scrollView.contentInsetAdjustmentBehavior = _savedContentInsetAdjustmentBehavior;
231
     }
239
     }
232
 #endif
240
 #endif
233
 
241
 
328
   _webView.backgroundColor = backgroundColor;
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
  * This method is called whenever JavaScript running within the web view calls:
356
  * This method is called whenever JavaScript running within the web view calls:
333
  *   - window.webkit.messageHandlers[MessageHandlerName].postMessage
357
  *   - window.webkit.messageHandlers[MessageHandlerName].postMessage

+ 17
- 0
ios/RNCWKWebViewManager.m View File

14
 @interface RNCWKWebViewManager () <RNCWKWebViewDelegate>
14
 @interface RNCWKWebViewManager () <RNCWKWebViewDelegate>
15
 @end
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
 @implementation RNCWKWebViewManager
30
 @implementation RNCWKWebViewManager
18
 {
31
 {
19
   NSConditionLock *_shouldStartLoadLock;
32
   NSConditionLock *_shouldStartLoadLock;
52
 RCT_EXPORT_VIEW_PROPERTY(cacheEnabled, BOOL)
65
 RCT_EXPORT_VIEW_PROPERTY(cacheEnabled, BOOL)
53
 RCT_EXPORT_VIEW_PROPERTY(allowsLinkPreview, BOOL)
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
  * Expose methods to enable messaging the webview.
73
  * Expose methods to enable messaging the webview.
57
  */
74
  */

+ 8
- 0
src/WebViewTypes.ts View File

260
   automaticallyAdjustContentInsets?: boolean;
260
   automaticallyAdjustContentInsets?: boolean;
261
   bounces?: boolean;
261
   bounces?: boolean;
262
   contentInset?: ContentInsetProp;
262
   contentInset?: ContentInsetProp;
263
+  contentInsetAdjustmentBehavior?: 'automatic'| 'scrollableAxes' | 'never' | 'always';
263
   dataDetectorTypes?: DataDetectorTypes | ReadonlyArray<DataDetectorTypes>;
264
   dataDetectorTypes?: DataDetectorTypes | ReadonlyArray<DataDetectorTypes>;
264
   decelerationRate?: number;
265
   decelerationRate?: number;
265
   directionalLockEnabled?: boolean;
266
   directionalLockEnabled?: boolean;
324
    */
325
    */
325
   automaticallyAdjustContentInsets?: boolean;
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
    * The amount by which the web view content is inset from the edges of
336
    * The amount by which the web view content is inset from the edges of
329
    * the scroll view. Defaults to {top: 0, left: 0, bottom: 0, right: 0}.
337
    * the scroll view. Defaults to {top: 0, left: 0, bottom: 0, right: 0}.