SwiftUI:通过 PageViewController 呈现模态

SwiftUI: present modal over PageViewController

我在使用 UIPageViewController 时遇到了一些问题。在其协调器中的某个时刻,对 UIViewControllers 的引用无效。它会导致在关闭模态视图后丢失除第一个页面之外的所有 UIPageViewController 页面。

完成基础 SwiftUI 教程Interfacing with UIKit the modal view presentation implemented on tap by featured item. There is github repo

我发现在 modalview 解散 pageViewController 中的协调器后,委托会查找最初创建的 UIViewController,而它们会被重新创建几次。因此,当我尝试滚动页面时,委托人找不到实际的 UIViewControllers


        func pageViewController(
            _ pageViewController: UIPageViewController,
            viewControllerAfter viewController: UIViewController) -> UIViewController?
        {
            guard let index = __parent.controllers.firstIndex(of: viewController)__ else {
                return nil
            }

        }

正如官方文档所说,协调器和 UIPageViewController 的同步应该由 UIViewControllerRepresentable 负责

    /// Updates the presented `UIViewController` (and coordinator) to the latest
    /// configuration.
    func updateUIViewController(_ uiViewController: Self.UIViewControllerType, context: Self.Context)

我是不是漏掉了什么,或者是 UIViewControllerRepresentable 中的这个错误?我如何在 UIPageViewController 上使用模态视图而不失去滚动页面的能力?

我有一个类似的问题,我通过调用 pageViewController.setViewControllers(_:direction:animated:)

解决了它

来自 makeUIViewController(context:) 函数而不是 updateUIViewController(:) 函数,因为前者只被调用一次。