问题
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.
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:
The PageViewController:
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
}
}
}
}
The PageView:
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)
}
}
The main ContentView:
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()
}
}
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?
回答1:
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)
}
}
来源:https://stackoverflow.com/questions/59448446/conditional-sibling-of-a-pageview-uipageviewcontroller-breaks-paging