5

How does one disable scroll bounce in a PDFView using PDFKit?

The view where the PDF is shown doesn't have a scroll bounce option.

Here's my code:

if let path = Bundle.main.path(forResource: pdfObject, ofType: "pdf") {
    let url = URL(fileURLWithPath: path)
    if let pdfDocument = PDFDocument(url: url) {

        pdfView.autoresizesSubviews = true
        pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight,
                                    .flexibleTopMargin, .flexibleLeftMargin]

        pdfView.autoScales = true
        pdfView.displaysPageBreaks = true
        pdfView.displayDirection = .vertical
        pdfView.displayMode = .singlePageContinuous
        pdfView.document = pdfDocument

        pdfView.maxScaleFactor = 4.0
        pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit        
    }
}

Thanks in advance (for what is likely a ridiculously simple question!)

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
Nicholas Farmer
  • 773
  • 7
  • 32

1 Answers1

5

Unfortunately, there isn't an exported API to set the PDFView desired bouncing behavior.

Having said that, you can (safely) exploit a PDFView implementation detail to hack your way around it for now:

extension PDFView {

    /// Disables the PDFView default bouncing behavior.
    func disableBouncing() {
        for subview in subviews {
            if let scrollView = subview as? UIScrollView {
                scrollView.bounces = false
                return
            }
        }
        print("PDFView.disableBouncing: FAILED!")
    }
}

and then use it like this in your code:

pdfView.disableBouncing()

Caveat. Please keep in mind that such solution might break in future iOS releases. Nevertheless, rest assured your app won't crash as a result (you only won't be disabling the bouncing behavior at all).

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
  • Hi Paulo, thanks for the reply. Unfortunately this doesn't have any effect. However thanks for letting me know I'm not missing a 'bounce' behavior attribute. Might have to wait for an update on PDFKit form apple. – Nicholas Farmer Mar 22 '19 at 13:22
  • @NicholasFarmer Hmm... I did a quick test here and was able to successfully disable the bouncing behavior on the `PDFView`. Will upload a short gist when I get a chance... – Paulo Mattos Mar 22 '19 at 15:24
  • Hey @NicholasFarmer, [run this code in a Swift Playground](https://gist.github.com/pmattos/f6adb5f2c03bb6c3a3ccfff340bd3d30) in Xcode (be sure the *Live View* is showing) and you will see that the *vertical scroll bouncing* is indeed disabled. Comment out the `pdfView.disableBouncing()` line to enable it back. – Paulo Mattos Mar 22 '19 at 20:24
  • 1
    Hi Paulo, apologies for the late reply. I went back and re-tested and still didn't work...however after recent update and upgrade to swift 5 - this now works beautifully. Many thanks for your input. Your answer has been accepted and upvoted. Kind regards – Nicholas Farmer Mar 31 '19 at 19:50
  • Hey @NicholasFarmer, glad to hear that ;) Good luck with your app! – Paulo Mattos Mar 31 '19 at 22:36