Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I center a PDFSelection within the overall View?

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): enter image description here

What I'd like to see (currentSelection in center of view): enter image description here

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).

like image 590
Daniel Avatar asked Jan 31 '26 16:01

Daniel


1 Answers

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
like image 172
Freddy Avatar answered Feb 02 '26 08:02

Freddy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!