【问题标题】:iOS 13.1 PDFKit PDFView.go(to page:) isn't workingiOS 13.1 PDFKit PDFView.go(to page:) 不工作
【发布时间】:2019-11-12 09:28:08
【问题描述】:

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 显示方向是水平的,看起来它可以垂直显示。

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    我尝试了这段示例代码,它可以让我将页面移动到下一页和上一页。您可以按照link 在 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)
    }
    }
    

    【讨论】:

    • 谢谢Usama,似乎在垂直显示方向上它有效,我尝试了垂直显示方向的代码并且导航有效。它在水平显示方向上对你有用吗?
    • @KeN 请添加这一行 pdfView.usePageViewController(true, withViewOptions: nil)
    • 嗯...但是请求需要是连续显示模式
    • 如果你喜欢的话,还有另一个更好的滑动手势stackoverflow.com/a/55114530
    猜你喜欢
    • 2012-03-22
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    相关资源
    最近更新 更多