WKWebView显示灰色背景,并且pdf内容在ViewController开关上不可见

Raj*_*rma 9 ipad ios swift wkwebview

我正在从WKWebView的主捆绑包或文档目录中加载pdf文件。它一开始会完美加载,但是如果在Tabbar Controller中切换选项卡后返回同一屏幕,则其显示的灰色背景和pdf内容将变得不可见。这是我用来加载的代码

class ViewController: UIViewController {

    @IBOutlet var progressView: UIProgressView!
    @IBOutlet var webView: WKWebView!

    var pdfURLS: URL?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        pdfURLS = Bundle.main.url(forResource: "97_pdf", withExtension: "pdf", subdirectory: nil, localization: nil)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        print("pdfURLS: \(String(describing: pdfURLS))")
    }

    @IBAction func loadAction(_ sender: Any) {
        if let pdfURL = pdfURLS  {
            self.webView.loadFileURL(pdfURL, allowingReadAccessTo: pdfURL.deletingLastPathComponent())
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有人对这个问题有想法吗?在此处输入图片说明

附有图像参考。如果我使用不推荐使用的UIWebView,则不会出现此类问题,但是我不想使用不推荐使用的库。

小智 12

我们在 Firefox for iOS 中也遇到了这个问题:

https://bugzilla.mozilla.org/show_bug.cgi?id=1516524

我还在 WebKit 错误中引用了这篇 SO 帖子:

https://bugs.webkit.org/show_bug.cgi?id=193281

暂时,我们找到了一个解决方法:

let previousZoomScale = webView.scrollView.zoomScale
let previousContentOffset = webView.scrollView.contentOffset

if let currentItem = webView.backForwardList.currentItem {
    webView.go(to: currentItem)
}

DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) {
    webView.scrollView.setZoomScale(previousZoomScale, animated: false)
    webView.scrollView.setContentOffset(previousContentOffset, animated: false)
}
Run Code Online (Sandbox Code Playgroud)

基本上,我们从重新绘制 PDF 的后退/前进历史列表中重新访问 URL,然后我们恢复滚动视图位置/缩放。它不是很好,但它有效。