2

The Apple API isn't working on iOS 13.1, does anyone have the same issue or I did wrong way to use it.

I tried to get a PDFPage from PDFDocument.page(at: validPageIndex), the outcome page is with right index, and I set this page to PDFView.go(to: page) and the navigation isn't working.

let validPageIndex: Int = 11
guard let targetPage = document.page(at: validPageIndex) else { return }
print(targetPage.index)
// Print 11
pdfView.go(to: targetPage)

the line pdfView.go(to: targetPage) was executed but the page in PDF file stay at first page

Thanks for Usama Azam, my PDFView display direction is horizontal, seems it works on vertical.

KeN
  • 21
  • 3

2 Answers2

2

I tried this sample piece of code and it works for me to move page next and previous. You can follow this link to add a PDF view in Storyboard.

import UIKit
import PDFKit

class ViewController: UIViewController {

@IBOutlet weak var pdfView: PDFView!
var currentPage = 0
override func viewDidLoad() {
    super.viewDidLoad()
    if let path = Bundle.main.path(forResource: "sample", ofType: "pdf") {
        if let pdfDocument = PDFDocument(url: URL(fileURLWithPath: path)) {
            pdfView.displayMode = .singlePageContinuous
            pdfView.autoScales = true
            pdfView.displayDirection = .vertical
            pdfView.document = pdfDocument
        }
    }
}

@IBAction func previousPage(_ sender: UIButton) {
    let validPageIndex: Int = currentPage - 1
    guard let targetPage = pdfView.document!.page(at: validPageIndex) else { return }
    print(targetPage.index)
    currentPage = currentPage - 1
    pdfView.go(to: targetPage)
}

@IBAction func nextPage(_ sender: UIButton) {
    let validPageIndex: Int = currentPage + 1
    guard let targetPage = pdfView.document!.page(at: validPageIndex) else { return }
    print(targetPage.index)
    currentPage = currentPage + 1
    pdfView.go(to: targetPage)
}
}
Usama Azam
  • 403
  • 5
  • 13
  • Thank you Usama, seems on vertical display direction it works, I tried my code with vertical display direction and the navigation works. Does it works for you on horizontal display direction? – KeN Nov 13 '19 at 02:12
  • 1
    @KeN please add this line pdfView.usePageViewController(true, withViewOptions: nil) – Usama Azam Nov 13 '19 at 06:13
  • Hmm... but the request needs it to be continuous display mode – KeN Nov 13 '19 at 08:41
  • There's another better idea of swipe gesture if you like it https://stackoverflow.com/a/55114530 – Usama Azam Nov 13 '19 at 09:10
0

Try this code, it solves the problem in the SwiftUI UIViewRepresentable.

        if let doc = pdfView.document,
           let page = doc.page(at: pageNumber)
        {
            Task { @MainActor in
                pdfView.go(to: page )
            }
        }