簡體   English   中英

iOS:如何清除 PDFView?

[英]iOS: How to clear PDFView?

我正在使用PDFView class 顯示一些 PDF 文件。 有一個問題是當我加載或更好地說替換另一個文件時,加載新文件時最后加載的文件仍然可見。

在此處輸入圖像描述

這是代碼:

var pdfView = PDFView()
//MARK: - PDF KIT
func previewPDF(url:URL) {
    
    if self.view.subviews.contains(pdfView) {
         self.pdfView.removeFromSuperview() // Remove it
     } else {
        
     }
    
    pdfView = PDFView(frame: PDFPreview.bounds)
    pdfView.removeFromSuperview()
    
    pdfView.backgroundColor = .clear
    pdfView.displayMode = .singlePage
    pdfView.autoScales = true
    pdfView.pageShadowsEnabled = false
    pdfView.document = PDFDocument(url: url)
    
    thumbnail = PDFThumbnail(url: url, width: 240)
    
    // I tried to nil PDFPreview, still nothing happened
    PDFPreview.addSubview(pdfView)
}

在這里,您將 pdfView 添加為 PDFPreview 的子視圖,但在第一次嘗試刪除它時,您正在檢查它是否存在於 self.view 的子視圖中,但實際上它位於 PDFPreview 的子視圖中 所以改成下面的代碼

func previewPDF(url:URL) {
    if PDFPreview.subviews.contains(pdfView) {
       self.pdfView.removeFromSuperview() // Remove it
     } else { 
   
   }

而且,當您第二次嘗試使用 removeFromSuperview() 刪除它時,您已經實例化了另一個 PDFView() 並丟失了對舊 PDFView 的引用,因此刪除舊 PDFView 也失敗了。

替代解決方案:如果您只是更改 pdf 文檔,更好的解決方案是更改 PDFView 的文檔屬性。 例如:

if let document = PDFDocument(url: path) {
    pdfView.document = document
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM