-1

I'm trying to display a pdf on an iOS device (ipad) using PDFkit. For some reason, swift displays the second page when the display opens. I figure out part of the problem is the preview.autoscale. When I set this to false, the pdf is not full screen, but it starts on the first page. So, I think the issue is the "zoom" part of the autoscale is zooming in on the first page. I did find a simalar question on stackoverflow (PDFKit: PDFView doesn't resize when device rotated), but the soution didnt work. Below is the relevant code:

    if let FBdocument = PDFDocument(url: fileURL) {
        pdfView.document = FBdocument
        pdfView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(pdfView)

        pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor,constant: 0.0).isActive = true
        pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor,constant: 0.0).isActive = true
        pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor,constant: 0.0).isActive = true

        pdfView.bottomAnchor.constraint(equalTo: letdothis_button.topAnchor,constant: 0.0).isActive = true


        pdfView.autoScales = true
K J
  • 8,045
  • 3
  • 14
  • 36
  • 1
    If you’re going to ask a question and someone responds, it is professional and simply polite to reply. If it solved your problem upvote and accept, if not, provide more info – Nicholas Farmer Nov 07 '18 at 14:33

2 Answers2

1

I was having issues with autoscales too, I found that moving:

pdfView.autoScales = true

Along with any other positioning methods to before:

pdfView.document = FBdocument

And then to add the subview last:

 view.addSubview(pdfView)

Thus I would use:

if let FBdocument = PDFDocument(url: fileURL) {

    pdfView.translatesAutoresizingMaskIntoConstraints = false

    pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor,constant: 0.0).isActive = true
    pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor,constant: 0.0).isActive = true
    pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor,constant: 0.0).isActive = true

    pdfView.bottomAnchor.constraint(equalTo: letdothis_button.topAnchor,constant: 0.0).isActive = true


    pdfView.autoScales = true

    pdfView.document = FBdocument

    view.addSubview(pdfView)
Nicholas Farmer
  • 773
  • 7
  • 32
  • I have to add the subview before I can add the constraints or else I get the error "Unable to activate constraint with anchors" – Henrik Hansen Sep 02 '19 at 13:42
0

This is a bug on iPad Only (working fine with iPhone), I did send a Bug Report and they responded it's duplicate of another report that mean they knew about it and hopefully, they will fix it soon

Basel
  • 550
  • 8
  • 21