hello i want to display a PDF file at page 7 in my SwiftUI app.
I would also like to use the functions .go(to :) and .currentPage() outside the UIViewRepresentable.
import SwiftUI
import PDFKit
struct PDFKitView: View {
var pdf: PDF
var body: some View {
PDFKitRepresentedView(pdf)
}
}
struct PDFKitRepresentedView: UIViewRepresentable {
let pdf: PDF
init(_ pdf: PDF) {
self.pdf = pdf
}
func makeUIView(context: UIViewRepresentableContext<PDFKitRepresentedView>) -> PDFKitRepresentedView.UIViewType {
let pdfView = PDFView()
let document = PDFDocument(data: pdf.content!)
pdfView.document = document
pdfView.autoScales = true
print("pdfView.currentPage: \(String(describing: pdfView.currentPage))") // pdfView.currentPage: Optional(<PDFPage: 0x600002245420> page index 0)
print("pdfView: \(pdfView)") // pdfView: <PDFView: 0x7fb206526c20; frame = (0 0; 0 0); gestureRecognizers = <NSArray: 0x600002ef4150>; layer = <CALayer: 0x60000202ddc0>>
if let myPage = document?.page(at: 7) {
pdfView.go(to: myPage)
}
print("pdfView.currentPage: \(String(describing: pdfView.currentPage))") // pdfView.currentPage: Optional(<PDFPage: 0x6000022455c0> page index 7)
print("pdfView: \(pdfView)") // pdfView: <PDFView: 0x7fb206526c20; frame = (0 0; 0 0); gestureRecognizers = <NSArray: 0x600002ef4150>; layer = <CALayer: 0x60000202ddc0>>
return pdfView
}
func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PDFKitRepresentedView>) {
// Update the view.
}
}
The PDF file is shown but at page 1 and not 7 :/
I'm still relatively new to it Swift, can anyone give me a hint?
this is how it works now
the problem was
and not
here the complete code