diff --git a/ios/PagerViewProvider.swift b/ios/PagerViewProvider.swift index 54fb5177..1b1e8754 100644 --- a/ios/PagerViewProvider.swift +++ b/ios/PagerViewProvider.swift @@ -91,6 +91,13 @@ import UIKit } } + override public func layoutSubviews() { + super.layoutSubviews() + if window != nil { + setupView() + } + } + @objc public func goTo(index: Int, animated: Bool) { if animated { withAnimation { @@ -106,19 +113,32 @@ import UIKit return } - self.hostingController = UIHostingController( + // Only create the hosting controller once it can actually be attached to the + // React view-controller hierarchy. `reactViewController()` can be nil on the + // first `didMoveToWindow` pass (e.g. when the pager is mounted inside a screen + // that is still being attached). Previously `self.hostingController` was + // assigned before this check, so when `reactViewController()` was nil the + // SwiftUI host was never added/pinned (its view frame stayed `.zero`, leaving + // the page blank) while the early-return above prevented any later retry. + // Deferring keeps `hostingController` nil so a subsequent window/layout pass + // can attach it once the view controller is available. + guard let parentViewController = reactViewController() else { + return + } + + let hostingController = UIHostingController( rootView: PagerView(props: props, delegate: delegate), ignoreSafeArea: true ) - if let hostingController, let parentViewController = reactViewController() { - parentViewController.addChild(hostingController) - hostingController.view.backgroundColor = .clear - addSubview(hostingController.view) + self.hostingController = hostingController - hostingController.view.translatesAutoresizingMaskIntoConstraints = false - hostingController.view.pinEdges(to: self) + parentViewController.addChild(hostingController) + hostingController.view.backgroundColor = .clear + addSubview(hostingController.view) - hostingController.didMove(toParent: parentViewController) - } + hostingController.view.translatesAutoresizingMaskIntoConstraints = false + hostingController.view.pinEdges(to: self) + + hostingController.didMove(toParent: parentViewController) } }