PageView (UIPageViewController) 的条件同级中断分页 [英] Conditional sibling of a PageView (UIPageViewController) breaks paging

查看:28
本文介绍了PageView (UIPageViewController) 的条件同级中断分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SwiftUI 中使用 这个 UIPageViewController 实现作为起点,但是当 currentPage 是最后一页时,我希望在 PageView 上方或下方显示一个视图(例如:确定"文本或按钮).

I'm using this UIPageViewController implementation in SwiftUI as a starting point, but I want to have a view (eg: "OK" Text or a Button) appear above or below the PageView when currentPage is the last page.

然而,每当我在 PageView 上方或下方添加 if 语句时,只要我滑动到第二页,所有其他页面都会被删除,并且无法在页面之间滑动.如果我去掉 if 逻辑,一切正常.代码如下:

However any time I add an if statement above or below the PageView, as soon as I swipe to the second page, all other pages get removed and it becomes impossible to swipe between pages. If I take out the if logic, everything works fine. Here's the code:

页面视图控制器:

struct PageViewController: UIViewControllerRepresentable {
    var controllers: [UIViewController]

    @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) {
        var dir = UIPageViewController.NavigationDirection.forward
        if let visibleViewController = pageViewController.viewControllers?.first,
            let nextPage = controllers.firstIndex(of: visibleViewController) {
            if currentPage < nextPage {
                dir = .reverse
            }
        }

        pageViewController.setViewControllers([controllers[currentPage]], direction: dir, animated: true)
    }

    class Coordinator: NSObject, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
        var parent: PageViewController

        init(_ pageViewController: PageViewController) {
            self.parent = pageViewController
        }

        func pageViewController(
            _ pageViewController: UIPageViewController,
            viewControllerBefore viewController: UIViewController) -> UIViewController?
        {
            guard let index = parent.controllers.firstIndex(of: viewController) else {
                return nil
            }
            if index == 0 {
                return nil
            }
            return parent.controllers[index - 1]
        }

        func pageViewController(
            _ pageViewController: UIPageViewController,
            viewControllerAfter viewController: UIViewController) -> UIViewController?
        {
            guard let index = parent.controllers.firstIndex(of: viewController) else {
                return nil
            }
            if index + 1 == parent.controllers.count {
                return nil
            }
            return parent.controllers[index + 1]
        }

        func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
            if completed,
                let visibleViewController = pageViewController.viewControllers?.first,
                let index = parent.controllers.firstIndex(of: visibleViewController)
            {
                parent.currentPage = index
            }
        }
    }
}

页面视图:

struct PageView<Page: View>: View {
    var viewControllers: [UIHostingController<Page>]
    @Binding var currentPage: Int

    init(_ views: [Page], currentPage: Binding<Int>) {
        self._currentPage = currentPage
        self.viewControllers = views.map { UIHostingController(rootView: $0) }
    }

    var body: some View {
        PageViewController(controllers: viewControllers, currentPage: $currentPage).frame(width: UIScreen.main.bounds.width)
    }
}

主要内容视图:

struct TestView: View {
    @State private var curPage: Int = 0

    var body: some View {
        ZStack {
            Color(.secondarySystemBackground)

            VStack {
                if curPage == 9892832 {
                    Text("OK").padding().foregroundColor(.blue).padding(.top, 60)
                }

                PageView([
                    Text("Page 1"),
                    Text("Page 2"),
                    Text("Page 3"),
                    Text("Page 4")
                ], currentPage: $curPage)
            }

        }.edgesIgnoringSafeArea(.all)
    }
}

struct TestView_Previews: PreviewProvider {
    static var previews: some View {
        TestView()
    }
}

我不知道这是 SwiftUI 错误还是使用 UIViewControllerRepresentable 的副作用或什么.有没有其他人看到这个带有条件视图?关于解决方法的任何想法?

I can't tell if this is a SwiftUI bug or a side effect of using UIViewControllerRepresentable or what. Is anyone else seeing this w/ conditional views? Any ideas on a workaround?

推荐答案

我不知道这个问题的根本原因是什么.但是我用两个嵌套的 VStack 解决了这个问题.将您的 PageView 放在内部,并将您的 if 或其他视图放在外部 VStack 中.

I don't know what the root cause of this issue is. But I solved it with two nested VStacks. Put your PageView in the inner one and put your if or other Views to the outer VStack.

VStack {
    if curPage == 9892832 {
                Text("OK").padding().foregroundColor(.blue).padding(.top, 60)
    }

    VStack {

        PageView([
                Text("Page 1"),
                Text("Page 2"),
                Text("Page 3"),
                Text("Page 4")
            ], currentPage: $curPage)
    }

}

这篇关于PageView (UIPageViewController) 的条件同级中断分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆