fix(ios): defer SwiftUI host setup until reactViewController is ready#1088
Open
IsaacIsrael wants to merge 1 commit into
Open
fix(ios): defer SwiftUI host setup until reactViewController is ready#1088IsaacIsrael wants to merge 1 commit into
IsaacIsrael wants to merge 1 commit into
Conversation
setupView() assigned self.hostingController before verifying that reactViewController() was non-nil. On the first didMoveToWindow pass the React view controller can still be nil, so the `if let ... reactViewController()` block was skipped and the SwiftUI hosting view was never added or pinned (its frame stayed .zero, rendering the page blank). Because self.hostingController was already set, the early-return guard then blocked every later retry, so the pager stayed blank for the lifetime of the view. Timing-dependent, so it reproduced intermittently. Guard on reactViewController() before creating/assigning the hosting controller, and retry setupView() from layoutSubviews() so attachment happens as soon as the view controller hierarchy is ready. Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1089
Summary
On the New Architecture iOS path,
PagerViewcan render a permanently blank page, intermittently, with no content visible even though children are mounted with valid frames.Root cause
In
PagerViewProvider.setupView(),self.hostingControlleris assigned before verifying thatreactViewController()is non-nil:setupView()is called fromdidMoveToWindow(). On the first passreactViewController()can still be nil (the pager is in a window but its screen/view-controller isn't attached yet). When that happens:if let … reactViewController()block is skipped, so the SwiftUI hosting view is never added as a subview and never pinned — its frame stays.zero, so the page is blank.self.hostingControlleris now non-nil, so on the nextdidMoveToWindow()(when the view controller is available) theif self.hostingController != nil { return }guard short-circuits and setup never retries.The result is a blank pager for the lifetime of the view. Because it depends on whether
reactViewController()happens to be ready on the first pass, it reproduces intermittently (more often when the pager is mounted during a navigation transition / inside a freshly-pushed screen).I confirmed this on a device by logging frames:
provider.boundswas correct (e.g.{390, 754}) whilehostingController.view.frameremained{0, 0, 0, 0}, and the failing pass loggedreactViewController() == nil.Implementation
setupView()now bails out early (leavinghostingControllernil) whenreactViewController()is nil, instead of half-initializing. The hosting controller is only created/assigned once it can actually be attached.layoutSubviews()override that callssetupView()while in-window, so attachment happens as soon as the view-controller hierarchy is ready. The existinghostingController != nilguard keeps subsequent calls a cheap no-op.This is a minimal, behavior-preserving change for the happy path (when
reactViewController()is available on the first pass, behavior is identical).Test Plan
What's required for testing (prerequisites)?
iOS, New Architecture enabled. A
PagerViewmounted inside a screen that is pushed/attached (e.g. behind a navigation transition or tab) so thatreactViewController()is nil on the firstdidMoveToWindow.What are the steps to reproduce (after prerequisites)?
PagerViewas part of its initial mount.Compatibility
Checklist
README.mdMade with Cursor