Search code examples
iosiphoneswiftpdfkit

Resize PDF View when device orientation changes


I have a ViewController in my app that allows the user to open a PDF. The app supports landscape and portrait orientation the of device. I am having a problem when the user changes the devices orientation after opening the PDF. The PDF has two pages: Page 1 - All pink and Page 2 - All purple

I am encountering a framing problem once the PDF is opened and the user changes the devices orientation. If the PDF is opened in a portrait orientation, the pdf only fills up half of the screen once the ordination of the device is changed to landscape: Portrait Orientation after being opened in a portrait orientation Landscape Orientation after being opened in portrait orientation

Likewise, if the PDF is opened in a Landscape orientation, the pdf will only show on half of the screen once the orientation of the device is portrait: Lanscape Orientation after being opened in a landscape Orientation Portrait Orientation after being opened in a landscape Orientation

Here is my code:

import UIKit
import PDFKit

class PDFfun: UIViewController {

    var pdfView = PDFView()

override func viewDidLoad() {
    super.viewDidLoad()

    pdfView = PDFView(frame: self.view.frame)

    if let documentURL = Bundle.main.url(forResource: "Blank", withExtension: "pdf"),
        let document = PDFDocument(url: documentURL),
        let _ = document.page(at: 0) {

        pdfView.document = document
        pdfView.autoScales = true
        pdfView.backgroundColor = UIColor.lightGray
        pdfView.autoScales = true

     }
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        if UIDevice.current.orientation.isLandscape {
            print("landscape")
            pdfView = PDFView(frame: view.frame)
            pdfView.autoScales = true
        } else {
            print("portait")
            pdfView = PDFView(frame: view.frame)
            pdfView.autoScales = true
        }
    }
}

I tired adding override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) fix my problem but I notice no change.


Solution

  • Best way is you add layout constraints to your pdfView. In your viewDidLoad for example:

       pdfView.translatesAutoresizingMaskIntoConstraints = false
       view.addSubview(pdfView)     
       view.addConstraints([
            NSLayoutConstraint(item: pdfView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0),
            NSLayoutConstraint(item: pdfView, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0),
            NSLayoutConstraint(item: pdfView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0),
            NSLayoutConstraint(item: pdfView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0)
            ])
    

    If your dont want to use layoutConstraint you also can set the frame for your pdfView in the viewDidLayoutSubviews method:

        override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    
             pdfView.frame = view.frame
        }
    

    And dont forget to remove your code inside the viewWillTransition method.