I'm trying to display a page overlay over a PDF using PDFKit's Page Overlay feature. My actual use case is to allow drawing input as per this WWDC tutorial, but for the sake of simplicity here, my example just displays a button as a test of user interaction.
The issue that I'm having is that the view overlay ignores user interaction completely, so the button doesn't work. I'm unsure if this is my lack of experience with UIKit or a bug in the new framework.
Testing on iOS 16.4 simulator and 16.3.1 iPad.
I've tried various iterations of isUserInteractionEnabled
, but again, I'm not sure whether this is my misunderstanding of UIKit or if it's an issue with the framework.
Here's my sample code:
import Foundation
import SwiftUI
import PDFKit
struct PDFViewer: UIViewRepresentable {
let document:PDFDocument
func makeUIView(context: Context) -> UIView {
let view = PDFView()
view.pageOverlayViewProvider = context.coordinator
view.usePageViewController(false)
view.document = document
return view
}
func updateUIView(_ pdfView: UIView, context: Context) {
}
func makeCoordinator() -> Coordinator {
Coordinator()
}
class Coordinator:NSObject, PDFPageOverlayViewProvider {
func pdfView(_ view: PDFView, overlayViewFor page: PDFPage) -> UIView? {
let button = MyView()
return button
}
}
}
MyView is just a UIView that contains a button with a print statement. I've tested that works fine in a separate view. I'm using the PDFViewer in a SwiftUI context as below:
struct PDFDisplayView: View {
let document = PDFDocument(url: Bundle.main.url(forResource: "example", withExtension: "pdf")!)!
var body: some View {
PDFViewer(document: document)
}
}