我使用WKWebView
预览Office-Documents PDF之类的附件。我的WKWebView
是UIPageViewController
的一部分,一切基本都能正常工作。
我看到的唯一问题是,当我开始拖动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