SwiftUI + UIPageViewController,多种视图类型

SwiftUI + UIPageViewController, multiple view types

我想在我的 SwiftUI 应用程序中使用 UIPageViewController,如 this tutorial 中所示。但是,教程传入的视图类型与页面相同,我想传入任何类型的视图。

struct PageViewController<Page: View>: UIViewControllerRepresentable {
    var pages: [Page]
    @Binding var currentPage: Int
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func makeUIViewController(context: Context) -> UIPageViewController {
        let pageViewController = UIPageViewController(
            transitionStyle: .scroll,
            navigationOrientation: .horizontal)
        pageViewController.delegate = context.coordinator
        return pageViewController
    }

    func updateUIViewController(_ pageViewController: UIPageViewController, context: Context) {
        pageViewController.setViewControllers(
            [context.coordinator.controllers[currentPage]], direction: .forward, animated: true)
    }
    
    class Coordinator: NSObject, UIPageViewControllerDelegate {
        var parent: PageViewController
        var controllers = [UIViewController]()
        
        init(_ pageViewController: PageViewController) {
            parent = pageViewController
            controllers = parent.pages.map { UIHostingController(rootView: [=10=]) }
        }
        
        func pageViewController(
            _ pageViewController: UIPageViewController,
            didFinishAnimating finished: Bool,
            previousViewControllers: [UIViewController],
            transitionCompleted completed: Bool) {
            if completed,
               let visibleViewController = pageViewController.viewControllers?.first,
               let index = controllers.firstIndex(of: visibleViewController) {
                parent.currentPage = index
            }
        }
    }
}


struct PageView<Page: View>: View {
    var pages: [Page]
    @State var currentPage = 0

    var body: some View {
        ZStack {
            PageViewController(pages: pages, currentPage: $currentPage)
        }
    }
}

struct PageView_Previews: PreviewProvider {
    static var previews: some View {
        PageView(pages: [
            Color.red,
            Color.blue 
            // I want to add other view types here
        ])
    }
}

如果我将 PageView_Previews 中的页面数组更改为 [Color.red, Text("abc")] 之类的内容,我会收到错误消息,因为它们不是同一类型。如何让 SwiftUI 在此处允许异构视图类型?

我不能将 TabView 与 PageTabViewStyle 一起使用(很多错误),所以这就是我需要使用此 UIKit 方法的原因。

您可以将页面包装在 AnyView:

PageView(pages: [
            AnyView(Color.red),
            AnyView(Color.blue),
            AnyView(Text("Hi"))
])