From 14fca34226c28ebe73751af3b6fce3ddd4e98931 Mon Sep 17 00:00:00 2001 From: Thanh Vu Date: Wed, 21 May 2025 12:36:02 +0700 Subject: [PATCH 1/3] Prevent invalidateSafeAreaInsets get call when RNCSafeAreaProviderComponentView still in the fabric view pool --- ios/Fabric/RNCSafeAreaProviderComponentView.mm | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ios/Fabric/RNCSafeAreaProviderComponentView.mm b/ios/Fabric/RNCSafeAreaProviderComponentView.mm index d16aea4a..9250b2b4 100644 --- a/ios/Fabric/RNCSafeAreaProviderComponentView.mm +++ b/ios/Fabric/RNCSafeAreaProviderComponentView.mm @@ -57,6 +57,7 @@ - (void)safeAreaInsetsDidChange - (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)) { @@ -123,6 +124,7 @@ - (void)prepareForRecycle _currentSafeAreaInsets = UIEdgeInsetsZero; _currentFrame = CGRectZero; _initialInsetsSent = NO; + [NSNotificationCenter.defaultCenter removeObserver:self]; } @end From 9a7b7d05bd6ab932b88ffd8f5dd74994cd1d4348 Mon Sep 17 00:00:00 2001 From: Thanh Vu Date: Thu, 22 May 2025 16:11:56 +0700 Subject: [PATCH 2/3] Register notifications when move to new superview --- .../RNCSafeAreaProviderComponentView.mm | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/ios/Fabric/RNCSafeAreaProviderComponentView.mm b/ios/Fabric/RNCSafeAreaProviderComponentView.mm index 9250b2b4..8a67822b 100644 --- a/ios/Fabric/RNCSafeAreaProviderComponentView.mm +++ b/ios/Fabric/RNCSafeAreaProviderComponentView.mm @@ -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 @@ -30,7 +31,21 @@ - (instancetype)initWithFrame:(CGRect)frame if (self = [super initWithFrame:frame]) { static const auto defaultProps = std::make_shared(); _props = defaultProps; + } + + 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) @@ -45,9 +60,6 @@ - (instancetype)initWithFrame:(CGRect)frame name:UIKeyboardDidChangeFrameNotification object:nil]; #endif - } - - return self; } - (void)safeAreaInsetsDidChange @@ -125,6 +137,7 @@ - (void)prepareForRecycle _currentFrame = CGRectZero; _initialInsetsSent = NO; [NSNotificationCenter.defaultCenter removeObserver:self]; + _registeredNotifications = NO; } @end From 2f2efb04444b3cd7fb6ac61a91f1b00457f51a8d Mon Sep 17 00:00:00 2001 From: Thanh Vu Date: Thu, 22 May 2025 21:11:15 +0700 Subject: [PATCH 3/3] Format code --- .../RNCSafeAreaProviderComponentView.mm | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/ios/Fabric/RNCSafeAreaProviderComponentView.mm b/ios/Fabric/RNCSafeAreaProviderComponentView.mm index 8a67822b..910ebb68 100644 --- a/ios/Fabric/RNCSafeAreaProviderComponentView.mm +++ b/ios/Fabric/RNCSafeAreaProviderComponentView.mm @@ -36,29 +36,31 @@ - (instancetype)initWithFrame:(CGRect)frame return self; } -- (void)willMoveToSuperview:(UIView*)newSuperView { - [super willMoveToSuperview:newSuperView]; +- (void)willMoveToSuperview:(UIView *)newSuperView +{ + [super willMoveToSuperview:newSuperView]; - if (newSuperView != nil && !_registeredNotifications) { - _registeredNotifications = YES; - [self registerNotifications]; - } + if (newSuperView != nil && !_registeredNotifications) { + _registeredNotifications = YES; + [self registerNotifications]; + } } -- (void)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]; + [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 } @@ -69,7 +71,9 @@ - (void)safeAreaInsetsDidChange - (void)invalidateSafeAreaInsets { - if (self.superview == nil) { return; } + 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)) {