Skip to content

Commit 9a7b7d0

Browse files
committed
Register notifications when move to new superview
1 parent 14fca34 commit 9a7b7d0

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

ios/Fabric/RNCSafeAreaProviderComponentView.mm

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ @implementation RNCSafeAreaProviderComponentView {
1717
UIEdgeInsets _currentSafeAreaInsets;
1818
CGRect _currentFrame;
1919
BOOL _initialInsetsSent;
20+
BOOL _registeredNotifications;
2021
}
2122

2223
// Needed because of this: https://github.com/facebook/react-native/pull/37274
@@ -30,7 +31,21 @@ - (instancetype)initWithFrame:(CGRect)frame
3031
if (self = [super initWithFrame:frame]) {
3132
static const auto defaultProps = std::make_shared<const RNCSafeAreaProviderProps>();
3233
_props = defaultProps;
34+
}
35+
36+
return self;
37+
}
38+
39+
- (void)willMoveToSuperview:(UIView*)newSuperView {
40+
[super willMoveToSuperview:newSuperView];
3341

42+
if (newSuperView != nil && !_registeredNotifications) {
43+
_registeredNotifications = YES;
44+
[self registerNotifications];
45+
}
46+
}
47+
48+
- (void)registerNotifications {
3449
#if !TARGET_OS_TV && !TARGET_OS_OSX
3550
[NSNotificationCenter.defaultCenter addObserver:self
3651
selector:@selector(invalidateSafeAreaInsets)
@@ -45,9 +60,6 @@ - (instancetype)initWithFrame:(CGRect)frame
4560
name:UIKeyboardDidChangeFrameNotification
4661
object:nil];
4762
#endif
48-
}
49-
50-
return self;
5163
}
5264

5365
- (void)safeAreaInsetsDidChange
@@ -125,6 +137,7 @@ - (void)prepareForRecycle
125137
_currentFrame = CGRectZero;
126138
_initialInsetsSent = NO;
127139
[NSNotificationCenter.defaultCenter removeObserver:self];
140+
_registeredNotifications = NO;
128141
}
129142

130143
@end

0 commit comments

Comments
 (0)