简体   繁体   中英

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: 以纵向打开后的纵向 纵向打开后的横向

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方向 在横向中打开后的人像方向

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM