[英]iOS 13.1 PDFKit PDFView.go(to page:) isn't working
Apple API 在 iOS 13.1 上不工作,有沒有人有同樣的問題或者我使用它的方式不對。
我試圖從 PDFDocument.page(at: validPageIndex) 獲取一個 PDFPage,結果頁面有正確的索引,我將此頁面設置為 PDFView.go(to: page) 並且導航不工作。
let validPageIndex: Int = 11
guard let targetPage = document.page(at: validPageIndex) else { return }
print(targetPage.index)
// Print 11
pdfView.go(to: targetPage)
執行了pdfView.go(to: targetPage)
行,但 PDF 文件中的頁面停留在第一頁
感謝 Usama Azam,我的 PDFView 顯示方向是水平的,似乎它適用於垂直方向。
我嘗試了這段示例代碼,它適用於我將頁面移動到下一頁和上一頁。 您可以按照此鏈接在 Storyboard 中添加 PDF 視圖。
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)
}
}
試試這段代碼,它解決了 SwiftUI UIViewRepresentable 中的問題。
if let doc = pdfView.document,
let page = doc.page(at: pageNumber)
{
Task { @MainActor in
pdfView.go(to: page )
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.