1

I have multiple PDFKit annotations added into my PDF Document. When the user selects the text annotations to add text, the keyboard appears and the document scrolls upward and the cursor is no long on the screen. I have tried treating the PDF Document as a textfield but that does not work. How can I keep the cursor on screen or centered above the keyboard when the user adds text on the PDF Annotation?

Here is my code:

import UIKit
import PDFKit

class PDFfun: UIViewController {

    var pdfView = PDFView()
    var textFieldMultiline3 = PDFAnnotation()
    let document = PDFDocument(url: Bundle.main.url(forResource: "DOCUMENT", withExtension: "pdf")!)


override func viewDidLoad() {
    super.viewDidLoad()

    pdfView = PDFView(frame: self.view.frame)



        pdfView.document = document
        pdfView.autoScales = true
        pdfView.backgroundColor = UIColor.lightGray
        self.insertMultilineTextBoxIntoOneOneNine(document!.page(at: 119)!)
        pdfView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(pdfView)
    view.addConstraints([
        NSLayoutConstraint(item: pdfView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0),
        NSLayoutConstraint(item: pdfView, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0),
        NSLayoutConstraint(item: pdfView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0),
        NSLayoutConstraint(item: pdfView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0)
        ])
}
func insertMultilineTextBoxIntoOneOneNine(_ page: PDFPage) {

    _ = page.bounds(for: .cropBox)

    let textFieldMultilineBounds3 = CGRect(x: 27, y: 50, width: 339, height: 510)
    textFieldMultiline3 = PDFAnnotation(bounds: textFieldMultilineBounds3, forType: PDFAnnotationSubtype(rawValue: PDFAnnotationSubtype.widget.rawValue), withProperties: nil)
    textFieldMultiline3.widgetFieldType = PDFAnnotationWidgetSubtype(rawValue: PDFAnnotationWidgetSubtype.text.rawValue)
    textFieldMultiline3.backgroundColor = UIColor.white.withAlphaComponent(1)
    textFieldMultiline3.font = UIFont(name: "Arial", size: 15)
    textFieldMultiline3.isMultiline = true
    page.addAnnotation(textFieldMultiline3)
}
 override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    if UIDevice.current.orientation.isLandscape {
        print("landscape")
        pdfView = PDFView(frame: view.frame)
        pdfView.autoScales = true
    } else {
        print("portait")
        pdfView = PDFView(frame: view.frame)
        pdfView.autoScales = true
    }
}
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    pdfView.frame = view.frame
}
override var prefersStatusBarHidden: Bool {
    return false
}
override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

}

voodoo
  • 109
  • 9

0 Answers0