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
38 changes: 29 additions & 9 deletions ios/PagerViewProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}