react / react/react-native

[iOS] TextInput `autoFocus` called in `didMoveToWindow` causing transition glitches with UIKit

Ouverte
#56,595 1 commentaire 13 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Component: TextInput Needs: Triage :mag: Platform: iOS
Langage dominant
C++
Étoiles
127k
Forks
25.3k
Merge moyen
1 j 23 h
PR mergées (30 j)
4

Description

Description

In react-native iOS apps its very common to have a setup where screens are represented as individual UIViewControllers. The most common case is when using:

There is a problem is with transitions between two UIViewController when the new screen contains a <TextInput autoFocus={true} />:

https://github.com/user-attachments/assets/cecca207-bc5f-47d8-a2cc-5e5304592879

There are three issues with the transition:

  • (Not very well visible in the video, but) there is a hang before the screen transition starts
  • The transition doesn't progress forward all the way. At one point it seems like its slightly reset back, before moving forward again
  • The keyboard changes its color weirdly from dark to light (i also noticed that's not visible in the video, you can see on the second video below)

Now, your first reaction might be that this is a bug with react-native-screens. It's not, let me explain.
The "problem" is that react-native TextInput implementation calls becomeFirstResponder in didMoveToWindow:

https://github.com/facebook/react-native/blob/fb0d28c6fd7dd3ed961f7d96c7072cf37b2d228d/packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm#L120-L127

If you try the same in a native iOS app, you can reproduce the issue! I created a sample native iOS app and tried this (the second transition in the recording is actually fine, so this is a bit of a race condition thing it seems):

https://github.com/user-attachments/assets/a621d2b8-2242-409f-b2bf-5c5144ed08d9

Code can be found here

It seems like this was added back in 2019/2020 to react-native, and back then it seemed to be the correct way to handle the transition correctly (ironically without that code it was causing the same issue i am reporting now)

And it seems like this was working fine, until iOS 16 came out. Since then new issue reports appeared on react-native-screens about this:

And in that sense, this seems to me a bit also like an iOS regression. Now, is this a bug we should just delegate to apple to fix in iOS?
I was contemplating about that, and I think it would be good to raise the issue, but at the same time it seems like its not 100% apple's fault.

After some research and talking to some iOS developers they all told me that you'd usually request focus for an input in viewDidAppear, not in didMoveToWindow. In didMoveToWindow we don't know if there is a transition is still in progress, where viewDidAppear seems to be the place we can be absolutely sure our view is present, and the VC fully appeared.
This does change the style of the transition a bit, as you will see the screen transition finish and only then the keyboard appears [but as a pay off it's never buggy]:

https://github.com/user-attachments/assets/2f2c53ce-87d8-4e8c-8f3d-427384ef4ee3

I checked all native iOS apps on my phone, including some of apple's, and it seems like all open the keyboard after the VC transition finished. I vaguely recall that when iOS 16 came out people were complaining that apple "broke the keyboard".

I will open the discussion on how we may want to address in the comments below.

Steps to reproduce
  1. Clone the reproducer
  2. Run it, click the button
React Native Version

0.85.2

Affected Platforms

Runtime - iOS

Output of npx @react-native-community/cli info
info Fetching system and libraries information...
System:
  OS: macOS 26.4.1
  CPU: (16) arm64 Apple M4 Max
  Memory: 117.55 MB / 48.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 22.14.0
    path: /nix/store/04fc23dsflkxl4s9p6lkigia1hq3vjp2-nodejs-22.14.0/bin/node
  Yarn:
    version: 1.22.22
    path: /Users/hannogoedecke/.yarn/bin/yarn
  npm:
    version: 10.9.2
    path: /nix/store/04fc23dsflkxl4s9p6lkigia1hq3vjp2-nodejs-22.14.0/bin/npm
  Watchman:
    version: 2024.03.11.00
    path: /Users/hannogoedecke/.nix-profile/bin/watchman
Managers:
  CocoaPods:
    version: 1.15.2
    path: /Users/hannogoedecke/.nix-profile/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 25.2
      - iOS 26.2
      - macOS 26.2
      - tvOS 26.2
      - visionOS 26.2
      - watchOS 26.2
  Android SDK: Not Found
IDEs:
  Android Studio: 2025.3 AI-253.30387.90.2532.14935130
  Xcode:
    version: 26.2/17C52
    path: /usr/bin/xcodebuild
Languages:
  Java: Not Found
  Ruby:
    version: 3.4.2
    path: /Users/hannogoedecke/.rbenv/shims/ruby
npmPackages:
  "@react-native-community/cli":
    installed: 20.1.0
    wanted: 20.1.0
  react:
    installed: 19.2.3
    wanted: 19.2.3
  react-native:
    installed: 0.85.2
    wanted: 0.85.2
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: true
  newArchEnabled: true
Stacktrace or Logs
No logs are relevant for this issue
MANDATORY Reproducer

https://github.com/hannojg/ios-textinput-autofocus-transition-uikit-issues

Screenshots and Videos

No response

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm, en particulier l’implémentation de didMoveToWindow, et exécutez le reproducer iOS obligatoire pour observer la transition. Comparez les pull requests historiques liés et déterminez un comportement convenu concernant le moment où le focus est appliqué ; le travail est considéré comme terminé lorsque la transition et l’apparition du clavier sont stables sans entraîner de régression de autoFocus.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
ios, objective-c, react-native
Domaine
mobile
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
Calme
Clarté
Plutôt claire
Accessibilité débutants
48/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.