Repository navigation
Description
Environment
- react-native-safe-area-context 5.11.1 (also 5.10.1; the provider view has had no window check since 5.7.0)
- react-native 0.88.1, New Architecture (Fabric)
- expo 58.0.22, expo-router 58.0.29 (
NativeTabs), react-native-screens 4.30.1 - iOS 27.0 simulator (iPhone 17), Release build
What happens
RNCSafeAreaProviderComponentView registers for UIKeyboardDidShowNotification, UIKeyboardDidHideNotification and UIKeyboardDidChangeFrameNotification on every instance, and each one calls invalidateSafeAreaInsets. That method checks superview and a zero frame, but not window. A view outside a window reports safeAreaInsets of zero. So one keyboard event makes every provider that is laid out but outside the window send { top: 0, … } to JS. JS keeps that value until the provider's next report.
Native tab containers produce exactly these providers. expo-router's iOS NativeTabs wraps each tab in its own SafeAreaProvider and mounts every tab at start-up (lazyByDefault: false). The tabs that have never been selected are laid out by Fabric but sit outside the window. After any keyboard event, the first open of each of those tabs paints with top: 0: content under the status bar. Then safeAreaInsetsDidChange sends the real inset and the content jumps down, 18–250 ms later across our runs.
Steps
- An app with a native tab container whose tabs each render their own
<SafeAreaProvider>, all tabs mounted at start-up (expo-routerNativeTabsdoes this by default on iOS). - Cold start on tab A.
- Focus any
TextInputon tab A, so the keyboard shows, then dismiss it. - Select tab B for the first time.
Expected: tab B's first frame uses the real top inset (62 on iPhone 17).
Actual: tab B's provider has already sent top: 0 (at step 3). Its first frame paints with top: 0, then jumps when safeAreaInsetsDidChange sends 62.
Without step 3, nothing jumps: an off-window provider gets no layout pass, so it sends nothing until it enters the window.
Reproduced on 5.11.1
Installed 5.11.1 unmodified (our patch reverted), ran pod install, built Release, and ran the steps above, scripted: the keyboard came from a text field in a sheet over tab A, then each other tab was selected for the first time.
- A one-line
console.loginSafeAreaProvider'sonInsetsChangelogged fourtop=0reports (one per never-selected tab, each with a parent that read 62) at the keyboard event, plus seven in-windowtop=62reports. - All four tabs' first frames painted their header under the status bar, and it dropped into place 18–68 ms later (frames from a screen recording).
- 5.10.1 gives the same result.
Suggested fix
The sibling RNCSafeAreaViewComponentView already guards this case in updateStateIfNecessary:
// A view detached from the window (e.g. a screen covered by a react-native-screens
// native stack) reports zero safeAreaInsets, which would commit a zero-inset layout
// for the hidden screen.
if (self.window == nil) {
return;
}The provider view needs the same check, plus a re-measure when it enters a window. safeAreaInsetsDidChange fires only when the value changes, so a provider whose in-window insets match the zero it held outside would otherwise never send its first report. This is the patch we ship (patch-package, against 5.10.1; 5.11.1 has the same code):
--- a/ios/Fabric/RNCSafeAreaProviderComponentView.mm
+++ b/ios/Fabric/RNCSafeAreaProviderComponentView.mm
@@ -46,6 +46,21 @@ using namespace facebook::react;
[self registerNotifications];
}
}
+
+- (void)didMoveToWindow
+{
+ [super didMoveToWindow];
+
+ if (self.window != nil) {
+ [self setNeedsLayout];
+ }
+}
#endif
- (void)registerNotifications
@@ -76,6 +91,15 @@ using namespace facebook::react;
if (self.superview == nil) {
return;
}
+#if !TARGET_OS_OSX
+ if (self.window == nil) {
+ return;
+ }
+#endifWe ask for a layout pass in didMoveToWindow instead of reading there, because at that point UIKit may not have updated safeAreaInsets yet. layoutSubviews already calls invalidateSafeAreaInsets. With the patch on the same build and the same steps, no provider sent 0 and every first open painted in place.
Side note: the re-measure on window entry probably also covers #774 (a recycled SafeAreaListener that never reports). We haven't verified that separately.
Related
- Fix proposal for the inset flicker (#364, #485): SafeAreaProvider emits a transient inset → 0 → inset across separate frames #738 and SafeAreaProvider never re-emits after a lost insets event — a transient 0 becomes a permanently stale useSafeAreaInsets value (Fabric, Android) #758 (transient
inset → 0 → insetemissions, mostly on Android) also end with a zero reaching JS. This report is a separate, deterministic iOS trigger: a provider that is laid out but outside a window measures zero when a keyboard notification arrives. Fixing one doesn't fix the other. - insets.top switching to 0 during iPad orientation change #485 (iOS
insets.top→ 0 on orientation change) may share this cause when the rotated provider is outside a window. We haven't checked that.
Happy to open a PR with this change if it's welcome.
Activity
floco-af commentedon Nov 4, 2026
Thanks for the detailed report and patch, @nicklamont. I reproduced this on an iOS 27 simulator with react-navigation native tabs (each tab in its own provider): on
maina never-selected tab's provider reportstop: 0at the keyboard event, then 116 when it enters the window. Thewindow == nilguard plus a re-measure indidMoveToWindowfixes it. I opened a draft PR with your change (credited as co-author): #807. Maintainers can decide if they would rather take a PR from you.