Reputation: 59
I have a PDF view and am not sure how to fix the zoom. It shows up on the UI as about 150% zoom when I would like it to fit the width of the screen. No edits to the UI sizing fixes the zoom issue so it must be fixed somewhere in the below struct. Thank you for your help!
struct PDFKitRepresentedView: UIViewRepresentable {
let url: URL
init(_ url: URL) {
self.url = url
}
func makeUIView(context: UIViewRepresentableContext<PDFKitRepresentedView>) -> PDFKitRepresentedView.UIViewType {
let pdfView = PDFView()
pdfView.document = PDFDocument(url: self.url)
return pdfView
}
func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PDFKitRepresentedView>) {
// Update the view.
}
}
struct PDFKitView: View {
var url: URL
var body: some View {
PDFKitRepresentedView(url)
}
}
Upvotes: 0
Views: 1532
Reputation: 52635
PDFView
exposes a few different properties you can use to alter how it's displayed. From the PDFView
headers:
// -------- scaling
// Method to get / set the current scaling on the displayed PDF document. Default is 1.0 (actual size).
// Note that the given scaleFactor is clamped by the current min / max scale factors.
// When using a page view controller layout on iOS, this only affects the currently visible page and
// is ignored for any future loaded pages. You can observe the PDFViewPageChangedNotification notification
// to see when new pages are visible to apply new scale factors to them.
open var scaleFactor: CGFloat
// Set the minimum and maximum scaling factors for the PDF document. Assigning these values will implicitly turn
// off autoScales, and allows scaleFactor to vary between these min / max scale factors
@available(iOS 11.0, *)
open var minScaleFactor: CGFloat
@available(iOS 11.0, *)
open var maxScaleFactor: CGFloat
// Toggles mode whereby the scale factor is automatically changed as the view is resized, or rotated, to maximize the
// PDF displayed. For continuous modes this is a "fit width" behavior, for non-continuous modes it is a "best fit" behavior.
open var autoScales: Bool
My first suggestion would just be try turning on/off autoScales
(pdfView.autoScales = true
) and see if that changes what you're perceiving. Then you can also experiment with manually setting the scaleFactor
Upvotes: 4