网址中的PDF无法在WKWebView中正确显示?

时间:2018-03-05 15:53:04

标签: ios swift pdf wkwebview wkwebviewconfiguration

我可以将HTML页面,YouTube视频等加载到此WKWebView并正确显示,但PDF会着色整个视图,在底部裁剪,并在向上滚动时覆盖导航栏的背景。我希望PDF只显示在导航栏下方的安全区域。

这是我的代码:

// add web view
let webConfiguration = WKWebViewConfiguration()
let customFrame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 0, height: self.view.frame.size.height))
let webView = WKWebView (frame: customFrame, configuration: webConfiguration)
webView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(webView)
webView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
webView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
webView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
webView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
webView.heightAnchor.constraint(equalTo: self.view.heightAnchor).isActive = true
webView.uiDelegate = self

// embed pdf in web view
let pdfUrl = URL(string: self.pdfUrlString)
let pdfRequest = URLRequest(url: pdfUrl!)
webView.load(pdfRequest)

我已尝试添加

webView.autoresizesSubviews = true
webView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
来自this Stack Overflow answer

并没有帮助。

以下是我看到的问题的屏幕截图:

Dark tinted background everywhere

PDF cut off at bottom and covering nav bar background

请帮忙!谢谢!

2 个答案:

答案 0 :(得分:0)

我也遇到了WKWebViews的问题(在某些情况下不显示PDF),因此我现在只使用UIWebViews。不建议使用UIWebViews,因此这不是一个理想的解决方案,但它现在可能是一个快速解决方案。

答案 1 :(得分:0)

我发现WKWebView在从网址中提取时不会显示PDF标记的问题。我相信这是一个类似的问题。仍然出现在iOS11.3.1上。我切换回UIWebView。直到最近我才使用UIWebView。