From 4429a05afc22c8280b50cc01ce5585b87401d743 Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Mon, 30 Nov 2026 13:03:38 +0200 Subject: [PATCH 1/2] fix(ios): attach the hosting controller outside view callbacks UIKit can be enumerating the parent view controller's children when the provider's view callbacks run, e.g. while it propagates safe area insets after a status bar change and a synchronous React mount adds a pager. Calling addChild/removeFromParent there throws "Collection was mutated while being enumerated". Attach, move and detach on the next main queue turn instead. --- ios/PagerViewProvider.swift | 64 ++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/ios/PagerViewProvider.swift b/ios/PagerViewProvider.swift index 61f02508..838e3eb3 100644 --- a/ios/PagerViewProvider.swift +++ b/ios/PagerViewProvider.swift @@ -15,6 +15,7 @@ public func createPagerViewProvider(delegate: PagerViewProviderDelegate) -> UIVi final class PagerViewProvider: UIView, RNCPagerViewProviding { private weak var delegate: PagerViewProviderDelegate? private var hostingController: UIHostingController? + private var isParentViewControllerSyncScheduled = false private var props = PagerViewProps() var scrollEnabled: Bool = true { @@ -75,25 +76,25 @@ final class PagerViewProvider: UIView, RNCPagerViewProviding { if window != nil { setupView() } else { - syncParentViewController(to: nil) + scheduleParentViewControllerSync() } } override func willMove(toWindow newWindow: UIWindow?) { super.willMove(toWindow: newWindow) if newWindow != nil { - syncParentViewController(to: reactViewController()) + scheduleParentViewControllerSync() } } override func willMove(toSuperview newSuperview: UIView?) { - syncParentViewController(to: newSuperview?.reactViewController()) super.willMove(toSuperview: newSuperview) + scheduleParentViewControllerSync() } override func didMoveToSuperview() { super.didMoveToSuperview() - syncParentViewController(to: reactViewController()) + scheduleParentViewControllerSync() } override func layoutSubviews() { @@ -277,15 +278,10 @@ final class PagerViewProvider: UIView, RNCPagerViewProviding { private func setupView() { if self.hostingController != nil { - syncParentViewController(to: reactViewController()) + scheduleParentViewControllerSync() 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 @@ -300,22 +296,60 @@ final class PagerViewProvider: UIView, RNCPagerViewProviding { ) 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) + scheduleParentViewControllerSync() + } + + /// The view controller the hosting controller should be a child of right + /// now: the nearest one in the responder chain while in a window, else + /// none. 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. + private func targetParentViewController() -> UIViewController? { + window != nil ? reactViewController() : nil + } + + /// Attaches, moves or detaches the hosting controller on the next main + /// queue turn rather than immediately. + /// + /// These view callbacks can run while UIKit is enumerating the very + /// `children` array the change would mutate. 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 this view. Calling + /// `addChild`/`removeFromParent` there throws "Collection was mutated while + /// being enumerated". + private func scheduleParentViewControllerSync() { + guard let hostingController, + hostingController.parent !== targetParentViewController(), + !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 + Self.sync(hostingController, to: self?.targetParentViewController()) + } } /// 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, + to parentViewController: UIViewController? + ) { + guard hostingController.parent !== parentViewController else { return } From 89afa055141ebda5d85cb3be2cf9fd76af2c9d6c Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Mon, 30 Nov 2026 13:41:43 +0200 Subject: [PATCH 2/2] fix(ios): re-parent between controllers right away UIKit checks the hosting controller's parent as soon as its view moves to a window, so moving it into a page controller that SwiftUI rebuilt around a nested pager can't wait (#1098). Only attaching to the first controller and detaching are deferred. --- ios/PagerViewProvider.swift | 64 +++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/ios/PagerViewProvider.swift b/ios/PagerViewProvider.swift index 838e3eb3..9673d4a2 100644 --- a/ios/PagerViewProvider.swift +++ b/ios/PagerViewProvider.swift @@ -76,25 +76,25 @@ final class PagerViewProvider: UIView, RNCPagerViewProviding { if window != nil { setupView() } else { - scheduleParentViewControllerSync() + updateParentViewController(to: nil) } } override func willMove(toWindow newWindow: UIWindow?) { super.willMove(toWindow: newWindow) if newWindow != nil { - scheduleParentViewControllerSync() + updateParentViewController(to: reactViewController()) } } override func willMove(toSuperview newSuperview: UIView?) { + updateParentViewController(to: newSuperview?.reactViewController()) super.willMove(toSuperview: newSuperview) - scheduleParentViewControllerSync() } override func didMoveToSuperview() { super.didMoveToSuperview() - scheduleParentViewControllerSync() + updateParentViewController(to: reactViewController()) } override func layoutSubviews() { @@ -278,7 +278,7 @@ final class PagerViewProvider: UIView, RNCPagerViewProviding { private func setupView() { if self.hostingController != nil { - scheduleParentViewControllerSync() + updateParentViewController(to: reactViewController()) return } @@ -302,43 +302,51 @@ final class PagerViewProvider: UIView, RNCPagerViewProviding { hostingController.view.translatesAutoresizingMaskIntoConstraints = false hostingController.view.pinEdges(to: self) - scheduleParentViewControllerSync() + updateParentViewController(to: reactViewController()) } - /// The view controller the hosting controller should be a child of right - /// now: the nearest one in the responder chain while in a window, else - /// none. 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. - private func targetParentViewController() -> UIViewController? { - window != nil ? reactViewController() : nil - } - - /// Attaches, moves or detaches the hosting controller on the next main - /// queue turn rather than immediately. + /// Makes `parentViewController` the hosting controller's parent. /// - /// These view callbacks can run while UIKit is enumerating the very - /// `children` array the change would mutate. For example, showing or hiding - /// the status bar makes UIKit walk the view controller tree to update safe - /// area insets (`_recursiveUpdateContentOverlayInsetsFromParentIfNecessary`). + /// 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 this view. Calling - /// `addChild`/`removeFromParent` there throws "Collection was mutated while - /// being enumerated". - private func scheduleParentViewControllerSync() { + /// 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 !== targetParentViewController(), - !isParentViewControllerSyncScheduled else { + 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 - Self.sync(hostingController, to: self?.targetParentViewController()) + // 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) } }