Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

406
Vistas
How to disable zoom in and zoom out in pdfView?

I want to have just what I receive after

 pdfView.autoScales = true

without the possibility to change the scale.

After the help of @carmine we have:

import UIKit
import PDFKit

class ViewController: UIViewController {

@IBOutlet weak var pdfView: PDFView!

override func viewDidLoad() {
    super.viewDidLoad()

    setPdfFile()
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    self.pdfView.autoScales = true
}

func setPdfFile() {
    if let path = Bundle.main.path(forResource: "Swift.org - Getting Started", ofType: "pdf") {
        let url = URL(fileURLWithPath: path)
        if let pdfDocument = PDFDocument(url: url) {
            pdfView.autoScales = true
            pdfView.displayMode = .singlePageContinuous
            pdfView.displayDirection = .vertical
            pdfView.document = pdfDocument

            pdfView.displaysPageBreaks = false

            pdfView.minScaleFactor = pdfView.scaleFactor
            pdfView.maxScaleFactor = pdfView.scaleFactorForSizeToFit
            }
        }
    }
}

This gives such a result: https://gfycat.com/briefpowerlessbug

But if you run without these lines:

            pdfView.minScaleFactor = pdfView.scaleFactor
            pdfView.maxScaleFactor = pdfView.scaleFactorForSizeToFit

You'll receive the result I need but with zooming: https://gfycat.com/presentconcretebunting

I want to disable zooming and make pdfView's width equal to pdf-file's width.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

I attempted various methods of disabling pinch zoom, including overriding canZoomIn (I found it was not called on pinch zoom), setting the delegate's pdfViewWillChangeScaleFactor method (I found it was never called), and setting the max and min scale factor on the view (this prevented the view from autoscaling).

In the end, I was able to accomplish what I wanted to by adding my own pinchGestureRecognizer to the PDFView to override the view's built in pinchGestureRecognizer. In order for your pinch recognizer to have precedence, you will need to create a delegate for your recognizer and override the following method:

 func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
         shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {

        //Prefer this pinch recognizer over the default one to override pinch zoom
        if gestureRecognizer is UIPinchGestureRecognizer && otherGestureRecognizer is UIPinchGestureRecognizer {
            return true
        }
    }
   return false
}
over 4 years ago · Santiago Trujillo Denunciar

0

Use

 pdfView.minScaleFactor = pdfView.scaleFactor
 pdfView.maxScaleFactor = pdfView.scaleFactorForSizeToFit

And add

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    self.pdfView.autoScales = true
}
over 4 years ago · Santiago Trujillo Denunciar

0

The solution is to create a CustomPDFView and override it's Instance Property canZoomIn :

class CustomPDFView: PDFView {
    override func canZoomIn() -> Bool {
        return false
    }
}

Then the @IBOutlet weak var pdfView: PDFView! should be of type @IBOutlet weak var pdfView: CustomPDFView!. Also update it on IdentyInspector so it will not crash you.

For more informmation please check: canZoomIn from Apple Docs

canZoomIn

Returns a Boolean value indicating whether the user can magnify the view and zoom in.

Declaration

var canZoomIn: Bool { get }
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda