iOS PDFkit Displaymode = Singlepage Only Shows the First Page of the PDF

ios PDFKit displaymode = singlepage only shows the first page of the pdf

A another simple way to do this is setting

pdfView.usePageViewController(true) 

This adds the swiping between pages for you and no need to set up your own gestures. See example below:

override func viewDidLoad() {
super.viewDidLoad()

// Add PDFView to view controller.
let pdfView = PDFView(frame: self.view.bounds)
self.view.addSubview(pdfView)

// Configure PDFView to be one page at a time swiping horizontally
pdfView.autoScales = true
pdfView.displayMode = .singlePage
pdfView.displayDirection = .horizontal
pdfView.usePageViewController(true)

// load PDF
let webUrl: URL! = URL(string: url)
pdfView.document = PDFDocument(url: webUrl!)
}

How to present a single page pdf document in PDFView so that the document is centred and fully presented?

I create the view from code the following way and it works (left and right side are aligned to superview left and right and height is scaled to fit)

var pdfView = PDFView(frame: view.frame)
pdfView.document = PDFDocument(url: Bundle.main.url(forResource: "doc"
, withExtension: "pdf")!)

pdfView.displayMode = .singlePage
pdfView.autoScales = true

view.addSubview(pdfView)

iOS PDF Kit - Full Size Page

Change the displayMode to single page for pdfView

pdfView.displayMode = .singlePage
pdfView.displayDirection = .vertical
pdfView.autoScales = true
//pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
pdfView.usePageViewController(true, withViewOptions: nil)

Page turning animation in ios pdfkit singlepage displaymode

enabling usePageController enabled UI page view controller scroll style scrolling

pdfView!.usePageViewController(true, withViewOptions: nil)

from here:
https://developer.apple.com/documentation/pdfkit/pdfview/2877501-usepageviewcontroller

iOS PDFKit adjust pdf frame in PDFView

New Hack:

So it turns out, after digging through some more private APIs (yes, this solution is still gross) that there is this magical property that I had completely overlooked on PDFScrollView (a private, internal view) called... /p>

forcesTopAlignment /p>

To enable this, find the PDFScrollView in your PDFView and:

pdfScrollView.setValue(true, forKey: "forcesTopAlignment")

That does the trick!



Old Hack:

After a few days up against the wall I finally figured out a way to get PDFView to behave how I want.

Disclosure: This solution uses method swizzling and relies on private view hierarchy and could break at any time. That said, it works for now and I'm happy with the solution.

The full solution is available in this gist. (The meat is in overrideCenterAlign.)

There is a private method aptly named _centerAlign which vertically centers and scales the pdf pages as they come onto the screen and as they're scaled with the pinch gesture. Swizzling that method allows me to inject my own logic and apply my own transforms to position the pdf view how I'd like (with the top of the page aligned to the top of the view.)

There are two cases to consider. The "short page" case (pages 1, 3, 4 in the example) and the "tall page" case (page 2 in the example.) In both cases I start by invoking the original implementation of _centerAlign so that it can apply its transforms and manage updating the internal scroll view.

  • For the "short page" case, I apply the same transform with a vertical translation to align the top of the pdf with the top of the view.
  • For the "long page" case, I adjust the internal scroll view's zoom scale as it comes onto the screen so that it's scaled to fit the width of the view.

All of that said, I'm open to cleaner solutions that don't rely on private methods and view hierarchy. If you know of a better way to accomplish this, please post an answer!



Related Topics



Leave a reply



Submit