Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 57 additions & 15 deletions ios/PagerViewProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
final class PagerViewProvider: UIView, RNCPagerViewProviding {
private weak var delegate: PagerViewProviderDelegate?
private var hostingController: UIHostingController<PagerView>?
private var isParentViewControllerSyncScheduled = false
private var props = PagerViewProps()

var scrollEnabled: Bool = true {
Expand Down Expand Up @@ -75,25 +76,25 @@
if window != nil {
setupView()
} else {
syncParentViewController(to: nil)
updateParentViewController(to: nil)
}
}

override func willMove(toWindow newWindow: UIWindow?) {
super.willMove(toWindow: newWindow)
if newWindow != nil {
syncParentViewController(to: reactViewController())
updateParentViewController(to: reactViewController())
}
}

override func willMove(toSuperview newSuperview: UIView?) {
syncParentViewController(to: newSuperview?.reactViewController())
updateParentViewController(to: newSuperview?.reactViewController())
super.willMove(toSuperview: newSuperview)
}

override func didMoveToSuperview() {
super.didMoveToSuperview()
syncParentViewController(to: reactViewController())
updateParentViewController(to: reactViewController())
}

override func layoutSubviews() {
Expand Down Expand Up @@ -277,15 +278,10 @@

private func setupView() {
if self.hostingController != nil {
syncParentViewController(to: reactViewController())
updateParentViewController(to: reactViewController())
return
}

// Window-level containers, such as FullWindowOverlay, do not have a
// controller in their responder chain. The hosting view can still render
// there, but must not be attached to an unrelated controller.
let parentViewController = reactViewController()

// The hosting view must not carry a safe area: `PagerView`'s `GeometryReader`
// is measured inside it, and every page is framed to that measurement, so the
// pages end up inset by the safe area while React Native's own layout still
Expand All @@ -300,22 +296,68 @@
)
self.hostingController = hostingController

parentViewController?.addChild(hostingController)
hostingController.view.backgroundColor = .clear
addSubview(hostingController.view)

hostingController.view.translatesAutoresizingMaskIntoConstraints = false
hostingController.view.pinEdges(to: self)

hostingController.didMove(toParent: parentViewController)
updateParentViewController(to: reactViewController())
}

/// Makes `parentViewController` the hosting controller's parent.
///
/// Moving it from one controller to another happens right away: SwiftUI can
/// rebuild the page controller around a nested pager, and UIKit checks the
/// hosting controller's parent as soon as its view moves to a window
/// (#1098).
///
/// Attaching it to its first controller and detaching it wait for the next
/// main queue turn. These view callbacks can run while UIKit is enumerating
/// the very `children` array that would change. For example, showing or
/// hiding the status bar makes UIKit walk the view controller tree to update
/// safe area insets (`_recursiveUpdateContentOverlayInsetsFromParentIfNecessary`).
/// A view's `safeAreaInsetsDidChange` in that walk can commit and mount a
/// React tree synchronously (an `unstable_Immediate` state update, such as
/// react-native-screens' SafeAreaView), mounting or unmounting this view.
/// Calling `addChild`/`removeFromParent` there throws "Collection was mutated
/// while being enumerated". A hosting controller without a parent passes
/// UIKit's hierarchy check, so its view can render in the meantime.
private func updateParentViewController(to parentViewController: UIViewController?) {
guard let hostingController,
hostingController.parent !== parentViewController else {
return
}

if hostingController.parent != nil, parentViewController != nil {
Self.sync(hostingController, to: parentViewController)
return
}

guard !isParentViewControllerSyncScheduled else {
return
}
isParentViewControllerSyncScheduled = true
// Holds the hosting controller strongly: an unmounted pager can be
// deallocated before this runs, and must still detach it then.
DispatchQueue.main.async { [weak self] in
self?.isParentViewControllerSyncScheduled = false
// Off-window, or inside a window-level container such as
// FullWindowOverlay that has no controller in its responder chain, the
// hosting controller stays detached.
let parentViewController = self?.window != nil ? self?.reactViewController() : nil
Self.sync(hostingController, to: parentViewController)
}

Check warning on line 350 in ios/PagerViewProvider.swift

View check run for this annotation

apex-sentinel-staging / Apex Sentinel

minor: Coalescing flag discards updateParentViewController requests that carry a specific parent

While `isParentViewControllerSyncScheduled` is true, `updateParentViewController` returns without recording the requested parent, and the pending block ignores the argument entirely, re-deriving it from `self.window` and `reactViewController()`. A removal request arriving after a detach was already scheduled in the same main-queue turn is dropped, so the eventual parent comes from whatever the window/responder chain reports later rather than from `newSuperview?.reactViewController()` at the `willMove(toSuperview:)` call site.
Comment thread
troZee marked this conversation as resolved.
Resolved
}

/// SwiftUI can recreate the page controller around this React view while
/// preserving the view itself. Keep the hosting controller attached to the
/// current page controller so UIKit's view-controller hierarchy stays valid.
private func syncParentViewController(to parentViewController: UIViewController?) {
guard let hostingController,
hostingController.parent !== parentViewController else {
private static func sync(
_ hostingController: UIHostingController<PagerView>,
to parentViewController: UIViewController?
) {
guard hostingController.parent !== parentViewController else {
return
}

Expand Down
Loading