我的 PDF 文档中添加了多个 PDFKit 注释。当用户选择文本注释添加文本时,键盘出现,文档向上滚动,光标不在屏幕上。我尝试将 PDF 文档视为文本字段,但这不起作用。当用户在 PDF 注释上添加文本时,如何将光标保持在屏幕上或在键盘上方居中?
这是我的代码:
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
}
}