gpt4 book ai didi

swift - 支持不同 SwiftUI View 类型的 UIHostingController 数组

转载 作者:行者123 更新时间:2023-12-05 02:45:13 25 4
gpt4 key购买 nike

我正在关注 https://developer.apple.com/tutorials/swiftui/interfacing-with-uikit我正在尝试将我的页面 View 附加到 UIHostingControllers 数组,并且当我这样做时一切正常:

PageView(pages: [Text("hi"), Text("hello!")])

当 View 类型不同时,我得到 “无法为表达式生成诊断;请提交错误报告”:

PageView(pages: [Text("hi"), SomeOtherView()])

这是我的代码:

页面 View Controller

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)")
}
}

最佳答案

可能有更聪明的方法来对 View 数组进行类型删除,但也可以使用 AnyView 包装不同类型的 View :

PageView(pages: [AnyView(Text("hi")), AnyView(SomeOtherView())])

关于swift - 支持不同 SwiftUI View 类型的 UIHostingController 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66057163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com