Skip to content

Commit bacfd92

Browse files
RSNarafacebook-github-bot
authored andcommitted
Implement 'automaticallyAdjustContentInsets' and 'contentInset' props
Summary: @public This diff introduces two new props: 1. **automaticallyAdjustContentInsets**: Controls whether to adjust the content inset for web views that are placed behind a navigation bar, tab bar, or toolbar. The default value is true. 1. **contentInset**: 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}. **Note:** There're some inconsistencies between how `UIWebView` and `WKWebView` render web pages with respect to the `contentInset` property. These two videos illustrate the problem: **UIWebView** [[ P58674349 | Playground.js ]] https://pxl.cl/9R9V **WKWebView** [[ P58674348 | Playground.js ]] https://pxl.cl/9R9W Here's a stack overflow answer describing the problem: https://stackoverflow.com/a/35472603. Reviewed By: shergin Differential Revision: D6432181 fbshipit-source-id: aee6dac65d28435381ebec90519474b4707c7bab
1 parent 215fa14 commit bacfd92

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

Diff for: React/Views/RCTWKWebView.h

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
3232
@property (nonatomic, assign) BOOL bounces;
3333
@property (nonatomic, assign) BOOL mediaPlaybackRequiresUserAction;
3434
@property (nonatomic, assign) WKDataDetectorTypes dataDetectorTypes;
35+
@property (nonatomic, assign) UIEdgeInsets contentInset;
36+
@property (nonatomic, assign) BOOL automaticallyAdjustContentInsets;
3537

3638
- (void)postMessage:(NSString *)message;
3739
- (void)injectJavaScript:(NSString *)script;

Diff for: React/Views/RCTWKWebView.m

+25-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
static NSString *const MessageHanderName = @"ReactNative";
66

7-
@interface RCTWKWebView () <WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandler, UIScrollViewDelegate>
7+
@interface RCTWKWebView () <WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandler, UIScrollViewDelegate, RCTAutoInsetsProtocol>
88
@property (nonatomic, copy) RCTDirectEventBlock onLoadingStart;
99
@property (nonatomic, copy) RCTDirectEventBlock onLoadingFinish;
1010
@property (nonatomic, copy) RCTDirectEventBlock onLoadingError;
@@ -29,6 +29,8 @@ - (instancetype)initWithFrame:(CGRect)frame
2929
super.backgroundColor = [UIColor clearColor];
3030
_bounces = YES;
3131
_scrollEnabled = YES;
32+
_automaticallyAdjustContentInsets = YES;
33+
_contentInset = UIEdgeInsetsZero;
3234
}
3335
return self;
3436
}
@@ -51,6 +53,13 @@ - (void)didMoveToWindow
5153
_webView.navigationDelegate = self;
5254
_webView.scrollView.scrollEnabled = _scrollEnabled;
5355
_webView.scrollView.bounces = _bounces;
56+
57+
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
58+
if ([_webView.scrollView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
59+
_webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
60+
}
61+
#endif
62+
5463
[self addSubview:_webView];
5564

5665
[self visitSource];
@@ -95,6 +104,21 @@ - (void)setSource:(NSDictionary *)source
95104
}
96105
}
97106

107+
- (void)setContentInset:(UIEdgeInsets)contentInset
108+
{
109+
_contentInset = contentInset;
110+
[RCTView autoAdjustInsetsForView:self
111+
withScrollView:_webView.scrollView
112+
updateOffset:NO];
113+
}
114+
115+
- (void)refreshContentInset
116+
{
117+
[RCTView autoAdjustInsetsForView:self
118+
withScrollView:_webView.scrollView
119+
updateOffset:YES];
120+
}
121+
98122
- (void)visitSource
99123
{
100124
// Check for a static html source first

Diff for: React/Views/RCTWKWebViewManager.m

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ - (UIView *)view
3030
RCT_EXPORT_VIEW_PROPERTY(allowsInlineMediaPlayback, BOOL)
3131
RCT_EXPORT_VIEW_PROPERTY(mediaPlaybackRequiresUserAction, BOOL)
3232
RCT_EXPORT_VIEW_PROPERTY(dataDetectorTypes, WKDataDetectorTypes)
33+
RCT_EXPORT_VIEW_PROPERTY(contentInset, UIEdgeInsets)
34+
RCT_EXPORT_VIEW_PROPERTY(automaticallyAdjustContentInsets, BOOL)
3335

3436
/**
3537
* Expose methods to enable messaging the webview.

0 commit comments

Comments
 (0)