Skip to content

[iOS, Fabric] A SafeAreaProvider outside a window sends zero insets on keyboard notifications #806

Open

Description

@nicklamont

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

  1. An app with a native tab container whose tabs each render their own <SafeAreaProvider>, all tabs mounted at start-up (expo-router NativeTabs does this by default on iOS).
  2. Cold start on tab A.
  3. Focus any TextInput on tab A, so the keyboard shows, then dismiss it.
  4. 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.log in SafeAreaProvider's onInsetsChange logged four top=0 reports (one per never-selected tab, each with a parent that read 62) at the keyboard event, plus seven in-window top=62 reports.
  • 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;
+  }
+#endif

We 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

Happy to open a PR with this change if it's welcome.

Activity

  1. floco-af commented on Nov 4, 2026

    @floco-af
    Contributor

    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 main a never-selected tab's provider reports top: 0 at the keyboard event, then 116 when it enters the window. The window == nil guard plus a re-measure in didMoveToWindow fixes 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions