Add TextView inside pdfView that sticks to a Page

54 views Asked by At

In iPhone default PDFView, user can add text to the pdf. That time after adding the textView it sticks with the pdf page and scrolls with the page.

I want to replicate this behaviour in my CustomPDFViewer. I've created a CustomPDFView, a class that used PDFKit pdfView.

In this class, CustomPDFViewer, I've added a UITapGestureRecognizer on the PDFView to catch the location. Then I'm creating the frame with respect of location that I got from tap gesture. (x, y position. I'm giving a height and width).

// location is CGPoint which I get from TapGesture
let xPoint = location.x - 100
let yPoint = location.y - 100
let frame = CGRect(x: xPoint, y: yPoint, width: 200, height: 70)

After that I'm adding the textView to the pdfView.

pdfView.addSubview(textBoxView)

This adds a textView to the pdfView but the issue is If I scroll the pdf, the page scrolls but the textView remains on that position.

I want to stick this textView on the page where user taps so that If that page scrolls up or down, the textView moves with that as well.

So I've tried -

  1. Add textView directly to the pdfView of PDFKit. But it seems adding subView to the pdfView is not possible.

  2. Tried to assign self (CustomPDFView) to the delegate of pdfView. but even if I assign this (CustomPDFView) as delegate if pdfView the scrollView delegate functions are not getting called in CustomPDFView. So I can not able to move the textView as per scrollView offset.

NB: I can save the contents of the textView as annotations to the pdf and can display that as sticky to the page. But the only problem is to make the textView sticky to the page

Is there any way to add the textView or any view to the pdfView as a sticky to pdfPage?

Expected Current
In iPhone how to add text in PDFs This is how it looks now. Not moving with page scroll
1

There are 1 answers

0
DonMag On

You should be able to do this by adding the text view to the pdf view's .documentView...

    // safely unwrap optional
    if let docView = pdfView.documentView {
        docView.addSubview(textBoxView)
    }
    

It will then scroll with the pdf page.