Skip to content

Commit 975572a

Browse files
authored
Prevent invalidateSafeAreaInsets get call when RNCSafeAreaProviderComponentView still in the fabric view pool (#629)
* Prevent invalidateSafeAreaInsets get call when RNCSafeAreaProviderComponentView still in the fabric view pool * Register notifications when move to new superview * Format code
1 parent 37e8765 commit 975572a

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

ios/Fabric/RNCSafeAreaProviderComponentView.mm

Lines changed: 34 additions & 15 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,33 +31,49 @@ - (instancetype)initWithFrame:(CGRect)frame
3031
if (self = [super initWithFrame:frame]) {
3132
static const auto defaultProps = std::make_shared<const RNCSafeAreaProviderProps>();
3233
_props = defaultProps;
33-
34-
#if !TARGET_OS_TV && !TARGET_OS_OSX
35-
[NSNotificationCenter.defaultCenter addObserver:self
36-
selector:@selector(invalidateSafeAreaInsets)
37-
name:UIKeyboardDidShowNotification
38-
object:nil];
39-
[NSNotificationCenter.defaultCenter addObserver:self
40-
selector:@selector(invalidateSafeAreaInsets)
41-
name:UIKeyboardDidHideNotification
42-
object:nil];
43-
[NSNotificationCenter.defaultCenter addObserver:self
44-
selector:@selector(invalidateSafeAreaInsets)
45-
name:UIKeyboardDidChangeFrameNotification
46-
object:nil];
47-
#endif
4834
}
4935

5036
return self;
5137
}
5238

39+
- (void)willMoveToSuperview:(UIView *)newSuperView
40+
{
41+
[super willMoveToSuperview:newSuperView];
42+
43+
if (newSuperView != nil && !_registeredNotifications) {
44+
_registeredNotifications = YES;
45+
[self registerNotifications];
46+
}
47+
}
48+
49+
- (void)registerNotifications
50+
{
51+
#if !TARGET_OS_TV && !TARGET_OS_OSX
52+
[NSNotificationCenter.defaultCenter addObserver:self
53+
selector:@selector(invalidateSafeAreaInsets)
54+
name:UIKeyboardDidShowNotification
55+
object:nil];
56+
[NSNotificationCenter.defaultCenter addObserver:self
57+
selector:@selector(invalidateSafeAreaInsets)
58+
name:UIKeyboardDidHideNotification
59+
object:nil];
60+
[NSNotificationCenter.defaultCenter addObserver:self
61+
selector:@selector(invalidateSafeAreaInsets)
62+
name:UIKeyboardDidChangeFrameNotification
63+
object:nil];
64+
#endif
65+
}
66+
5367
- (void)safeAreaInsetsDidChange
5468
{
5569
[self invalidateSafeAreaInsets];
5670
}
5771

5872
- (void)invalidateSafeAreaInsets
5973
{
74+
if (self.superview == nil) {
75+
return;
76+
}
6077
// This gets called before the view size is set by react-native so
6178
// make sure to wait so we don't set wrong insets to JS.
6279
if (CGSizeEqualToSize(self.frame.size, CGSizeZero)) {
@@ -123,6 +140,8 @@ - (void)prepareForRecycle
123140
_currentSafeAreaInsets = UIEdgeInsetsZero;
124141
_currentFrame = CGRectZero;
125142
_initialInsetsSent = NO;
143+
[NSNotificationCenter.defaultCenter removeObserver:self];
144+
_registeredNotifications = NO;
126145
}
127146

128147
@end

0 commit comments

Comments
 (0)