diff --git a/ios/PagerViewProvider.swift b/ios/PagerViewProvider.swift index 61f02508..9673d4a2 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) + 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() { @@ -277,15 +278,10 @@ final class PagerViewProvider: UIView, RNCPagerViewProviding { 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 @@ -300,22 +296,68 @@ 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) + 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) + } } /// 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 }