Open PDF file using swift

前端 未结 11 2028
萌比男神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:19

    You can use this code in Swift 4

    1. Import PDFKit
    2. Copy this code

      let pdfView = PDFView()        
      pdfView.translatesAutoresizingMaskIntoConstraints = false
      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: "test", withExtension: "pdf") else { return }
      
      if let document = PDFDocument(url: path) {
          pdfView.document = document
      }
      

提交回复
热议问题