@@ -17,6 +17,7 @@ @implementation RNCSafeAreaProviderComponentView {
17
17
UIEdgeInsets _currentSafeAreaInsets;
18
18
CGRect _currentFrame;
19
19
BOOL _initialInsetsSent;
20
+ BOOL _registeredNotifications;
20
21
}
21
22
22
23
// Needed because of this: https://github.com/facebook/react-native/pull/37274
@@ -30,33 +31,49 @@ - (instancetype)initWithFrame:(CGRect)frame
30
31
if (self = [super initWithFrame: frame]) {
31
32
static const auto defaultProps = std::make_shared<const RNCSafeAreaProviderProps>();
32
33
_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
48
34
}
49
35
50
36
return self;
51
37
}
52
38
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
+
53
67
- (void )safeAreaInsetsDidChange
54
68
{
55
69
[self invalidateSafeAreaInsets ];
56
70
}
57
71
58
72
- (void )invalidateSafeAreaInsets
59
73
{
74
+ if (self.superview == nil ) {
75
+ return ;
76
+ }
60
77
// This gets called before the view size is set by react-native so
61
78
// make sure to wait so we don't set wrong insets to JS.
62
79
if (CGSizeEqualToSize (self.frame .size , CGSizeZero)) {
@@ -123,6 +140,8 @@ - (void)prepareForRecycle
123
140
_currentSafeAreaInsets = UIEdgeInsetsZero;
124
141
_currentFrame = CGRectZero;
125
142
_initialInsetsSent = NO ;
143
+ [NSNotificationCenter .defaultCenter removeObserver: self ];
144
+ _registeredNotifications = NO ;
126
145
}
127
146
128
147
@end
0 commit comments