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