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.
This commit is contained in:
aarondail 2019-08-09 01:55:35 -07:00 committed by Thibault Malbranche
parent f0df15efc5
commit 493b65de21
4 changed files with 68 additions and 1 deletions

View File

@ -36,6 +36,7 @@ This document lays out the current public properties and methods for the React N
- [`bounces`](Reference.md#bounces)
- [`overScrollMode`](Reference.md#overscrollmode)
- [`contentInset`](Reference.md#contentinset)
- [`contentInsetAdjustmentBehavior`](Reference.md#contentInsetAdjustmentBehavior)
- [`dataDetectorTypes`](Reference.md#datadetectortypes)
- [`scrollEnabled`](Reference.md#scrollenabled)
- [`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
---
### `contentInsetAdjustmentBehavior`
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`.
Possible values:
- `automatic`
- `scrollableAxes`
- `never`
- `always`
| Type | Required | Platform |
| ------ | -------- | -------- |
| string | No | iOS |
---
### `dataDetectorTypes`
Determines the types of data converted to clickable URLs in the web view's content. By default only phone numbers are detected.

View File

@ -49,6 +49,10 @@ static NSURLCredential* clientAuthenticationCredential;
BOOL _isFullScreenVideoOpen;
UIStatusBarStyle _savedStatusBarStyle;
BOOL _savedStatusBarHidden;
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
UIScrollViewContentInsetAdjustmentBehavior _savedContentInsetAdjustmentBehavior;
#endif
}
- (instancetype)initWithFrame:(CGRect)frame
@ -65,6 +69,10 @@ static NSURLCredential* clientAuthenticationCredential;
_savedKeyboardDisplayRequiresUserAction = YES;
_savedStatusBarStyle = RCTSharedApplication().statusBarStyle;
_savedStatusBarHidden = RCTSharedApplication().statusBarHidden;
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
_savedContentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
#endif
}
if (@available(iOS 12.0, *)) {
@ -227,7 +235,7 @@ static NSURLCredential* clientAuthenticationCredential;
}
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
if ([_webView.scrollView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
_webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
_webView.scrollView.contentInsetAdjustmentBehavior = _savedContentInsetAdjustmentBehavior;
}
#endif
@ -328,6 +336,22 @@ static NSURLCredential* clientAuthenticationCredential;
_webView.backgroundColor = backgroundColor;
}
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
- (void)setContentInsetAdjustmentBehavior:(UIScrollViewContentInsetAdjustmentBehavior)behavior
{
_savedContentInsetAdjustmentBehavior = behavior;
if (_webView == nil) {
return;
}
if ([_webView.scrollView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
CGPoint contentOffset = _webView.scrollView.contentOffset;
_webView.scrollView.contentInsetAdjustmentBehavior = behavior;
_webView.scrollView.contentOffset = contentOffset;
}
}
#endif
/**
* This method is called whenever JavaScript running within the web view calls:
* - window.webkit.messageHandlers[MessageHandlerName].postMessage

View File

@ -14,6 +14,19 @@
@interface RNCWKWebViewManager () <RNCWKWebViewDelegate>
@end
@implementation RCTConvert (UIScrollView)
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
RCT_ENUM_CONVERTER(UIScrollViewContentInsetAdjustmentBehavior, (@{
@"automatic": @(UIScrollViewContentInsetAdjustmentAutomatic),
@"scrollableAxes": @(UIScrollViewContentInsetAdjustmentScrollableAxes),
@"never": @(UIScrollViewContentInsetAdjustmentNever),
@"always": @(UIScrollViewContentInsetAdjustmentAlways),
}), UIScrollViewContentInsetAdjustmentNever, integerValue)
#endif
@end
@implementation RNCWKWebViewManager
{
NSConditionLock *_shouldStartLoadLock;
@ -52,6 +65,10 @@ RCT_EXPORT_VIEW_PROPERTY(applicationNameForUserAgent, NSString)
RCT_EXPORT_VIEW_PROPERTY(cacheEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(allowsLinkPreview, BOOL)
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
RCT_EXPORT_VIEW_PROPERTY(contentInsetAdjustmentBehavior, UIScrollViewContentInsetAdjustmentBehavior)
#endif
/**
* Expose methods to enable messaging the webview.
*/

View File

@ -260,6 +260,7 @@ export interface IOSNativeWebViewProps extends CommonNativeWebViewProps {
automaticallyAdjustContentInsets?: boolean;
bounces?: boolean;
contentInset?: ContentInsetProp;
contentInsetAdjustmentBehavior?: 'automatic'| 'scrollableAxes' | 'never' | 'always';
dataDetectorTypes?: DataDetectorTypes | ReadonlyArray<DataDetectorTypes>;
decelerationRate?: number;
directionalLockEnabled?: boolean;
@ -324,6 +325,13 @@ export interface IOSWebViewProps extends WebViewSharedProps {
*/
automaticallyAdjustContentInsets?: boolean;
/**
* 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.
*/
contentInsetAdjustmentBehavior?: 'automatic'| 'scrollableAxes' | 'never' | 'always'
/**
* The amount by which the web view content is inset from the edges of
* the scroll view. Defaults to {top: 0, left: 0, bottom: 0, right: 0}.