13

I am trying to move my project that was using a webview to display some pdf to a pdfView to take advantage of the latest PDFKit features.

in the webview when pinching to zoom out the document was always scaling to fill the screen. basically you could not zoom out the page it was bouncing back to fill the screen.

Now with a pdfView, I can zoom out by pinching and it does not look good at all there is no need to have the pdf page to be smaller than the screen...

Is there any way to activate the autoscale once you release your fingers from the screen. I know there is the gesture func but I am not familiar with its use.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Julien7377
  • 475
  • 4
  • 15
  • to answer my own question, it was actually very easy... pdfView.autoScales = true pdfView.maxScaleFactor = 4.0 pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit – Julien7377 Nov 16 '17 at 03:51
  • Kindly add your comment as your answer , as this might be helpful to others too. – Saurabh Prajapati Nov 22 '17 at 08:28

5 Answers5

23

Just to confirm that the accepted answer is the correct one, but also to highlight where the code needs to be used (as stated in the comment by answer author). i.e. the code must be used AFTER setting the pdf document:

pdfView.document = pdfDocument
pdfView.autoScales = true
pdfView.maxScaleFactor = 4.0
pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit 
Nicholas Farmer
  • 773
  • 7
  • 32
  • 1
    I just want to say that I love you. I was wasting so much time with this! Thanks for the help. – Andres Sep 27 '18 at 14:19
10

to answer my own question, it was actually very easy...

pdfView.autoScales = true 
pdfView.maxScaleFactor = 4.0 
pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
Julien7377
  • 475
  • 4
  • 15
  • 9
    I would like to add that this only worked for me after I set the document on the pdfView. If one sets the document after making those adjustments it does not have any effect. – christopher.online Feb 28 '18 at 00:27
1

This solution will automatically adjust the PDFView once you set the document property. Just use NoZoomOutPDFView instead of PDFView as your view for displaying a PDFDocument.

import Foundation
import PDFKit

final class NoZoomOutPDFView: PDFView {

    init() {
        super.init(frame: .zero)
        NotificationCenter
            .default
            .addObserver(
                self,
                selector: #selector(update),
                name: .PDFViewDocumentChanged,
                object: nil
            )
    }

    deinit {
        // If your app targets iOS 9.0 and later the following line can be omitted
        NotificationCenter.default.removeObserver(self)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    @objc private func update() {
        // PDF can be zoomed in but not zoomed out
        DispatchQueue.main.async {
            self.autoScales = true
            self.maxScaleFactor = 4.0
            self.minScaleFactor = self.scaleFactorForSizeToFit
        }
    }
}
christopher.online
  • 2,614
  • 3
  • 28
  • 52
  • 1
    Actually, since iOS 9.0 it is not necessary to remove observer from Notification Center. [documentation](https://developer.apple.com/documentation/foundation/nsnotificationcenter/1413994-removeobserver?language=objc) – Anton Vlasov Jan 10 '19 at 12:22
1

I had the same problem and the accepted answer did not work for me. scaleFactorForSizeToFit always returned 0.0, and therefore I was still able to zoom out until nothing was visible anymore. I don't know if this is specific to my application or something changed in iOS, but I had to trigger a layout update on the PDFView before setting the scale factor. This is how it works for me on iOS15:

   if let document = PDFDocument(url: URL(string: "https://www.adobe.com/pdf/pdfs/ISO32000-1PublicPatentLicense.pdf")!) {
        pdfView.displayDirection = .vertical
        pdfView.autoScales = true
        pdfView.document = document
        pdfView.setNeedsLayout()
        pdfView.layoutIfNeeded()
        pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
        pdfView.maxScaleFactor = 4.0
    }
Apfelsaft
  • 5,766
  • 4
  • 28
  • 37
0

If overriding PDFView's method is restricted, it can be done with adding an observer;

NotificationCenter.default.addObserver(self, selector: #selector(scaleChanged), name: NSNotification.Name.PDFViewScaleChanged, object: nil)

and using it like:

@objc func scaleChanged() {
    self.pdfView.maxScaleFactor = 3.0
    self.pdfView.minScaleFactor = self.pdfView.scaleFactorForSizeToFit
}
Kaan Esin
  • 70
  • 7