简体   繁体   中英

iOS 13.1 PDFKit PDFView.go(to page:) isn't working

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM