title: Swift 4: Change / Clear PDFView content
body: <p>I want to use PDFView framework in Swift 4. (<a href="https://developer.apple.com/documentation/pdfkit/pdfview" rel="nofollow noreferrer">https://developer.apple.com/documentation/pdfkit/pdfview</a>)</p>
  
  <p>The following function receives a path to a PDF document. If the path is valid, the PDF file is successful shown. A problem occurs, when I call <code>openMe(path: String)</code> twice. In this case, the old content is still there and the new content is added. I just want to change the old content with the new content.</p>
  
  <pre><code>private var pdfData: NSData? = nil
  
  func openMe(path: String) {
     let fileManager = FileManager.default
  
     if fileManager.fileExists(atPath: path){
         let url = NSURL.fileURL(withPath: path)
         pdfData = NSData(contentsOfFile: path)
         let pdfView = PDFView(frame: self.view.frame)
         pdfViewController?.pdfViewControllerInformsMeasurementDataViewController = self
         pdfView.document = PDFDocument(url: url)
         pdfView.autoScales = true
         pdfView.maxScaleFactor = 0.5
         pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
         pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  
         self.pdfViewController?.view.addSubview(pdfView)
         self.show(self.pdfViewController!, sender: nil)
      }
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Refer to excitedmicrobe's answer:
  I just changed the code like shown in the answer but the distance between the navigation controller and the PDFView differs.</p>
  
  <p>Fist <code>openMe</code> call:</p>
  
  <p><a href="https://i.stack.imgur.com/CNh1V.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CNh1V.png" alt="enter image description here"></a></p>
  
  <p>Second <code>openMe</code> call:</p>
  
  <p><a href="https://i.stack.imgur.com/94T8w.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/94T8w.png" alt="enter image description here"></a></p>