UIPageViewController中的WKWebView轻扫即可卸载

时间:2019-07-15 06:59:27

标签: ios wkwebview uipageviewcontroller

我使用WKWebView预览Office-Documents PDF之类的附件。我的WKWebViewUIPageViewController的一部分,一切基本都能正常工作。

我看到的唯一问题是,当我开始拖动PageViewController时,当前可见的WKWebView卸载。而且我不知道是什么原因?至少我不告诉这么做...

有人以前见过这个问题吗?希望得到任何提示。也供选择。 QLPreview不起作用,因为它要求我的附件事先在本地可用。

以下是一些代码:

import WebKit

final class ViewController: UIPageViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let theOneSingleViewController = ContentViewController()
        setViewControllers([theOneSingleViewController], direction: .forward, animated: false, completion: nil)

        dataSource = self
        delegate = self
    }
}

extension ViewController: UIPageViewControllerDataSource, UIPageViewControllerDelegate {

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        return nil
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        return nil
    }

    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        // we don't care
    }
}

final class ContentViewController: UIViewController {

    let webView = WKWebView(frame: .zero)


    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white

        view.addSubview(webView)
        webView.translatesAutoresizingMaskIntoConstraints = false

        let bindings = ["webView": webView]
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[webView]|", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: bindings))
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[webView]|", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: bindings))

        let url = URL(fileURLWithPath: Bundle.main.path(forResource: "Sample-PDF", ofType: "pdf")!)
        webView.loadFileURL(url, allowingReadAccessTo: url)
    }
}

示例代码也可以从以下网址下载:https://www.dropbox.com/s/raspcqpc92fia1t/WebViewTest.zip?dl=0

0 个答案:

没有答案