Swift PDF 查看器缩放?

问题描述

我有一个 PDF 视图,但不确定如何修复缩放。当我希望它适合屏幕宽度时,它在 UI 上显示为大约 150% 的缩放比例。对 UI 大小的任何编辑都不会修复缩放问题,因此它必须在以下结构中的某处修复。感谢您的帮助!

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)
    }
}

解决方法

PDFView 公开了一些不同的属性,您可以使用它们来改变它的显示方式。从 PDFView 标题:

// -------- 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

我的第一个建议是尝试打开/关闭 autoScales (pdfView.autoScales = true),看看这是否会改变您的感知。然后您还可以尝试手动设置 scaleFactor