Mariana Gomes
Mariana Gomes

Reputation: 81

How to make pdfView zoom=100% to fit screen size

So I have this pdfView and I wanna make it so its first display is a zoom of 100% of the page, but the problem is that what I have now is zooming in to much and the UI is not pretty to be like that.

My code is this:

    let pdfView = PDFView()

    pdfView.translatesAutoresizingMaskIntoConstraints = false
    pdfView.autoScales = true
    pdfView.maxScaleFactor = 4.0
    pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
    view.addSubview(pdfView)

    pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
    pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
    pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
    pdfView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
    
    
    guard let path = Bundle.main.url(forResource: materia , withExtension: "pdf") else { return }

    if let document = PDFDocument(url: path) {
        pdfView.document = document
    }

How I want it to be

How it is

Upvotes: 8

Views: 3520

Answers (1)

Marius Tanasoiu
Marius Tanasoiu

Reputation: 701

I know that it sounds crazy but all you have to do is to put pdfView.autoScales = true after pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit

My snipet:

        let pdfViewer = PDFView();
        
        if let path = Bundle.main.url(forResource: "test", withExtension: "pdf") {
            pdfViewer.document = PDFDocument(url: path);
        }
        
        pdfViewer.maxScaleFactor = 4.0;
        pdfViewer.minScaleFactor = pdfViewer.scaleFactorForSizeToFit;
        pdfViewer.autoScales = true;
       
        
        self.view.addSubview(pdfViewer);
        pdfViewer.translatesAutoresizingMaskIntoConstraints = false;
        
        pdfViewer.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor, constant: 0).isActive = true;
        pdfViewer.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true;
        pdfViewer.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true;
        pdfViewer.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true;

Upvotes: 16

Related Questions