支持不同 SwiftUI 视图类型的 UIHostingController 数组 [英] Array of UIHostingController supporting different SwiftUI View types
本文介绍了支持不同 SwiftUI 视图类型的 UIHostingController 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在关注 https://developer.apple.com/tutorials/swiftui/interface-with-uikit 并且我正在尝试将我的页面视图附加到 UIHostingControllers 数组中,而当我这样做时一切正常:
I'm following https://developer.apple.com/tutorials/swiftui/interfacing-with-uikit and I'm trying to append my page views to an array of UIHostingControllers and while everything works when I do:
PageView(pages: [Text("hi"), Text("hello!")])
我收到 "无法生成表达式的诊断信息;当视图类型不同时,请提交错误报告
:
PageView(pages: [Text("hi"), SomeOtherView()])
这是我的代码:
Here is my code:
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.dataSource = context.coordinator
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, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
var parent: PageViewController
var controllers = [UIViewController]()
init(_ pageViewController: PageViewController) {
parent = pageViewController
controllers = parent.pages.map { UIHostingController(rootView: $0.navigationBarHidden(true)) }
for viewController in controllers {
viewController.view.backgroundColor = .clear
}
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let index = controllers.firstIndex(of: viewController) else { return nil }
//swiping back on page 0
if index == 0 {
return nil
}
return controllers[index - 1]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let index = controllers.firstIndex(of: viewController) else { return nil }
//swiping forward on last page
if index + 1 == controllers.count {
return nil
}
return controllers[index + 1]
}
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 private var currentPage = 0
var body: some View {
PageViewController(pages: pages, currentPage: $currentPage)
Text("Current page = \(currentPage)")
}
}
推荐答案
可能有一种更聪明的方法来对视图数组进行类型擦除,但同样有效的方法是用 AnyView
:
There may be a more clever way to do the type erasure on the array of views, but what will also work is just wrapping the different typed Views with AnyView
:
PageView(pages: [AnyView(Text("hi")), AnyView(SomeOtherView())])
这篇关于支持不同 SwiftUI 视图类型的 UIHostingController 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文