Prabhakar Kasi
Prabhakar Kasi

Reputation: 529

PDFView - How to autoscale and still load first page?

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

Upvotes: 0

Views: 766

Answers (3)

Daniel Smith
Daniel Smith

Reputation: 960

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

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

Upvotes: 1

David
David

Reputation: 81

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.

Upvotes: 1

Prabhakar Kasi
Prabhakar Kasi

Reputation: 529

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

Upvotes: 1

Related Questions