Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 34 additions & 15 deletions ios/Fabric/RNCSafeAreaProviderComponentView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ @implementation RNCSafeAreaProviderComponentView {
UIEdgeInsets _currentSafeAreaInsets;
CGRect _currentFrame;
BOOL _initialInsetsSent;
BOOL _registeredNotifications;
}

// Needed because of this: https://github.com/facebook/react-native/pull/37274
Expand All @@ -30,33 +31,49 @@ - (instancetype)initWithFrame:(CGRect)frame
if (self = [super initWithFrame:frame]) {
static const auto defaultProps = std::make_shared<const RNCSafeAreaProviderProps>();
_props = defaultProps;

#if !TARGET_OS_TV && !TARGET_OS_OSX
[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(invalidateSafeAreaInsets)
name:UIKeyboardDidShowNotification
object:nil];
[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(invalidateSafeAreaInsets)
name:UIKeyboardDidHideNotification
object:nil];
[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(invalidateSafeAreaInsets)
name:UIKeyboardDidChangeFrameNotification
object:nil];
#endif
}

return self;
}

- (void)willMoveToSuperview:(UIView *)newSuperView
{
[super willMoveToSuperview:newSuperView];

if (newSuperView != nil && !_registeredNotifications) {
_registeredNotifications = YES;
[self registerNotifications];
}
}

- (void)registerNotifications
{
#if !TARGET_OS_TV && !TARGET_OS_OSX
[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(invalidateSafeAreaInsets)
name:UIKeyboardDidShowNotification
object:nil];
[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(invalidateSafeAreaInsets)
name:UIKeyboardDidHideNotification
object:nil];
[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(invalidateSafeAreaInsets)
name:UIKeyboardDidChangeFrameNotification
object:nil];
#endif
}

- (void)safeAreaInsetsDidChange
{
[self invalidateSafeAreaInsets];
}

- (void)invalidateSafeAreaInsets
{
if (self.superview == nil) {
return;
}
// This gets called before the view size is set by react-native so
// make sure to wait so we don't set wrong insets to JS.
if (CGSizeEqualToSize(self.frame.size, CGSizeZero)) {
Expand Down Expand Up @@ -123,6 +140,8 @@ - (void)prepareForRecycle
_currentSafeAreaInsets = UIEdgeInsetsZero;
_currentFrame = CGRectZero;
_initialInsetsSent = NO;
[NSNotificationCenter.defaultCenter removeObserver:self];
_registeredNotifications = NO;
}

@end
Expand Down