Open PDF file using swift

前端 未结 11 2029
萌比男神i
萌比男神i 2020-11-28 04:50

How can I add a PDF file for an app , where you click on a button to view the file & when you\'re done you get back to screen you were at?

11条回答
  •  情书的邮戳
    2020-11-28 05:42

    Apple added PDFKit framework in iOS 11

    Add a UIView to your view controller and make it's class to PDFView

    import UIKit
    import PDFKit
    
    class ViewController: UIViewController {
    
        @IBOutlet var pdfView: PDFView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            if let path = Bundle.main.path(forResource: "sample", ofType: "pdf") {
                if let pdfDocument = PDFDocument(url: URL(fileURLWithPath: path)) {
                    pdfView.displayMode = .singlePageContinuous
                    pdfView.autoScales = true
                    pdfView.displayDirection = .vertical
                    pdfView.document = pdfDocument
                }
            }
        }
    }
    

    There are 4 display modes : singlePage, singlePageContinuous, twoUp, twoUpContinuous .

提交回复
热议问题