I'm trying to use PDFKit with Swift in iOS 11. I have a PDFView and a PDFSelection that I would like to scroll to, zoom in to, and center within the overall view. I'm able to scroll to the PDFView's currentSelection with the function scrollSelectionToVisible(_ sender: Any?), but the selection always appear in the upper left of the screen.
How can I reposition the page to make the selection be at the center?
I know a PDFSelection has a bounds(for: PDFPage) function that returns a CGRect, in page space. But I don't know what to do with it beyond that.
What I currently see with after pdfView.scrollSelectionToVisible() (currentSelection in green upper left):

What I'd like to see (currentSelection in center of view):

This:
pdfView.currentSelection = selection
pdfView.scrollSelectionToVisible(nil)
And this:
pdfView.currentSelection = selection
let pdfPage = pdfView.document!.page(at: 0)!
let cgRect = pdfView.currentSelection!.bounds(for: pdfPage)
pdfView.go(to: cgRect, on: pdfPage)
are producing the same effect (the effect in the first image).
After you create the PDFviewer center the view like this
let pdfView = PDFView(frame: viewFA.view.bounds)
pdfView.center.y = viewFA.view.center.y - 100
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With