How to go to next page using PDFKit (if possible)

Hi !

I have implemented a button:

Button(">"){}
.onTapGesture {
      pdfView.goToNextPage(self)
}

to go to the next page. However, it doesn't work ?

  • Is this the correct way of triggering an action ?
  • How can I disable the button if I am on the last page (and prevent from using it) ?

Full code:

import SwiftUI
import PDFKit

struct TestPDFViewNavigation: View {
    let pdfDoc: PDFDocument
    let pdfView: PDFView
    
    init(pdfDoc: PDFDocument) {
        self.pdfDoc = pdfDoc
        self.pdfView = PDFView()
        self.pdfView.document = pdfDoc
    }
    
    var body: some View {
        PDFKitView(
            pdfView: pdfView
        )
    }
    
    struct ControlsView: View {
        let pdfView: PDFView
        let forward: Bool
        
        init(pdfView: PDFView, forward: Bool) {
            self.pdfView = pdfView
            self.forward = forward
        }
        
        var body: some View {
            Text(">")
            .onTapGesture {
                pdfView.goToNextPage(self)
            }
        }
    }

    // PDFKitView :: PDFView -> View
    struct PDFKitView: View {
        let pdfView: PDFView
        
        init(pdfView: PDFView) {
            self.pdfView = pdfView
        }

        var body: some View {
            VStack {
                ControlsView(pdfView: pdfView, forward: true)
                PDFKitRepresentedView(pdfView: pdfView)
            }
        }
    }

    // PDFKitRepresentedView :: PDFView -> UIViewRepresentable
    struct PDFKitRepresentedView: UIViewRepresentable {
        let pdfView: PDFView

        init(pdfView: PDFView) {
            self.pdfView = pdfView
        }

        func makeUIView(context: Context) -> UIView {
            return {
                pdfView.autoScales = true
                pdfView.displayDirection = .horizontal
                pdfView.usePageViewController(true)
                return pdfView
            }()
        }

        func updateUIView(_ uiView: UIView, context: Context) {
            guard let pdfView = uiView as? PDFView else {
                return
            }
        }

    }
}

let samplePDF = PDFDocument(url: Bundle.main.url(forResource: "MacBook", withExtension: "pdf")!)

struct TestPDFViewNavigation_Previews: PreviewProvider {
    static var previews: some View {
        TestPDFViewNavigation(pdfDoc: samplePDF!)
    }
}

//struct ContentView_Previews: PreviewProvider {
//    static var previews: some View {
//        TestPDFViewNavigation()
//    }
//}