0

If autoScales is disabled pdfView shows the 1st page (image 1) but if it is enabled PDFview scrolls to second page (image 2) on iPhoneX.

Github - https://github.com/2raptor/DownloadPDF

import UIKit
import PDFKit

class PDFViewController: UIViewController {
    var pdfView = PDFView()
    var pdfURL: URL?

    override func viewDidLoad() {
        super.viewDidLoad()

        pdfView.translatesAutoresizingMaskIntoConstraints = false
        if let pdfURL = pdfURL,
            let document = PDFDocument(url: pdfURL) {
            pdfView.document = document
            pdfView.autoScales = true
        }

        view.addSubview(pdfView)

        // Add contstraints
        pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
        pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
        pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
        pdfView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
    }
}

Tried few solutions but didn't work.

// after `pdfView.document = document` above
            if let firstPage = document.page(at: 0) {
                let firstPageBounds = firstPage.bounds(for: pdfView.displayBox)
                pdfView.go(to: CGRect(x: 0, y: firstPageBounds.height, width: 1.0, height: 1.0), on: firstPage)
            }

Prabhakar Kasi
  • 531
  • 4
  • 18

3 Answers3

1

Setting usePageViewController before setting autoScales solved the issue

pdfView.usePageViewController(true)

Complete solution that works

import UIKit
import PDFKit

class PDFViewController: UIViewController {
    var pdfView = PDFView()
    var pdfURL: URL?

    override func viewDidLoad() {
        super.viewDidLoad()

        pdfView.translatesAutoresizingMaskIntoConstraints = false
        if let pdfURL = pdfURL,
            let document = PDFDocument(url: pdfURL) {
            pdfView.document = document
            pdfView.usePageViewController(true)
            pdfView.autoScales = true
        }

        view.addSubview(pdfView)

        // Add contstraints
        pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
        pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
        pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
        pdfView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
    }
}
Prabhakar Kasi
  • 531
  • 4
  • 18
  • Unfortunately, this will not result in a single continuous page as in your original post. – David Jan 20 '21 at 15:24
  • That's correct. But I didn't have the requirement of showing single page at a time, so I was okay with this solution. I followed this article https://medium.com/@ji3g4kami/make-a-ebook-reader-with-pdfkit-in-swift-6010f82bd51 Please see if this helps. – Prabhakar Kasi Jan 27 '21 at 23:28
1

In my case there was a bug in iOS 12.4 with pdfView.autoScales = true and there was not much that I could do. Try upgrading to iOS 14.3 to see if it fixes this.

David
  • 81
  • 1
  • 6
1

Solution: https://stackoverflow.com/a/66565702/6404249

I can't copy-paste answer for you, but i hope it will helps you!

Daniel Smith
  • 720
  • 7
  • 13