Fix SafeAreaView on iOS < 11

Fix #13

From: https://github.com/facebook/react-native/pull/18534
This commit is contained in:
Bruno Lemos
2018-12-20 23:09:37 -02:00
parent ea18ee329a
commit 46a7942e72

View File

@@ -13,3 +13,137 @@ patch-package
ENTRY_FILE=${1:-index.ios.js}
else
ENTRY_FILE=${1:-index.js}
--- a/node_modules/react-native/React/Views/SafeAreaView/RCTSafeAreaView.h
+++ b/node_modules/react-native/React/Views/SafeAreaView/RCTSafeAreaView.h
@@ -1,5 +1,5 @@
/**
- * Copyright (c) 2015-present, Facebook, Inc.
+ * Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
@@ -17,6 +17,8 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithBridge:(RCTBridge *)bridge;
+@property (nonatomic, assign) BOOL emulateUnlessSupported;
+
@end
NS_ASSUME_NONNULL_END
--- a/node_modules/react-native/React/Views/SafeAreaView/RCTSafeAreaView.m
+++ b/node_modules/react-native/React/Views/SafeAreaView/RCTSafeAreaView.m
@@ -1,5 +1,5 @@
/**
- * Copyright (c) 2015-present, Facebook, Inc.
+ * Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
@@ -21,6 +21,7 @@
{
if (self = [super initWithFrame:CGRectZero]) {
_bridge = bridge;
+ _emulateUnlessSupported = YES; // The default.
}
return self;
@@ -29,7 +30,45 @@
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)decoder)
RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame)
+- (BOOL)isSupportedByOS
+{
+ return [self respondsToSelector:@selector(safeAreaInsets)];
+}
+
+- (UIEdgeInsets)safeAreaInsetsIfSupportedAndEnabled
+{
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
+ if (self.isSupportedByOS) {
+ return self.safeAreaInsets;
+ }
+#endif
+ return self.emulateUnlessSupported ? self.emulatedSafeAreaInsets : UIEdgeInsetsZero;
+}
+
+- (UIEdgeInsets)emulatedSafeAreaInsets
+{
+ UIViewController* vc = self.reactViewController;
+
+ if (!vc) {
+ return UIEdgeInsetsZero;
+ }
+
+ CGFloat topLayoutOffset = vc.topLayoutGuide.length;
+ CGFloat bottomLayoutOffset = vc.bottomLayoutGuide.length;
+ CGRect safeArea = vc.view.bounds;
+ safeArea.origin.y += topLayoutOffset;
+ safeArea.size.height -= topLayoutOffset + bottomLayoutOffset;
+ CGRect localSafeArea = [vc.view convertRect:safeArea toView:self];
+ UIEdgeInsets safeAreaInsets = UIEdgeInsetsMake(0, 0, 0, 0);
+ if (CGRectGetMinY(localSafeArea) > CGRectGetMinY(self.bounds)) {
+ safeAreaInsets.top = CGRectGetMinY(localSafeArea) - CGRectGetMinY(self.bounds);
+ }
+ if (CGRectGetMaxY(localSafeArea) < CGRectGetMaxY(self.bounds)) {
+ safeAreaInsets.bottom = CGRectGetMaxY(self.bounds) - CGRectGetMaxY(localSafeArea);
+ }
+
+ return safeAreaInsets;
+}
static BOOL UIEdgeInsetsEqualToEdgeInsetsWithThreshold(UIEdgeInsets insets1, UIEdgeInsets insets2, CGFloat threshold) {
return
@@ -41,23 +80,44 @@ static BOOL UIEdgeInsetsEqualToEdgeInsetsWithThreshold(UIEdgeInsets insets1, UIE
- (void)safeAreaInsetsDidChange
{
- if (![self respondsToSelector:@selector(safeAreaInsets)]) {
- return;
- }
+ [self invalidateSafeAreaInsets];
+}
+
+- (void)invalidateSafeAreaInsets
+{
+ [self setSafeAreaInsets:self.safeAreaInsetsIfSupportedAndEnabled];
+}
- UIEdgeInsets safeAreaInsets = self.safeAreaInsets;
+- (void)layoutSubviews
+{
+ [super layoutSubviews];
+
+ if (!self.isSupportedByOS && self.emulateUnlessSupported) {
+ [self invalidateSafeAreaInsets];
+ }
+}
+- (void)setSafeAreaInsets:(UIEdgeInsets)safeAreaInsets
+{
if (UIEdgeInsetsEqualToEdgeInsetsWithThreshold(safeAreaInsets, _currentSafeAreaInsets, 1.0 / RCTScreenScale())) {
return;
}
_currentSafeAreaInsets = safeAreaInsets;
- RCTSafeAreaViewLocalData *localData =
- [[RCTSafeAreaViewLocalData alloc] initWithInsets:safeAreaInsets];
+ RCTSafeAreaViewLocalData *localData = [[RCTSafeAreaViewLocalData alloc] initWithInsets:safeAreaInsets];
[_bridge.uiManager setLocalData:localData forView:self];
}
-#endif
+- (void)setEmulateUnlessSupported:(BOOL)emulateUnlessSupported
+{
+ if (_emulateUnlessSupported == emulateUnlessSupported) {
+ return;
+ }
+
+ _emulateUnlessSupported = emulateUnlessSupported;
+
+ [self invalidateSafeAreaInsets];
+}
@end
\ No newline at end of file