【问题标题】:Cannot load/scroll full pdf in swift WebView.无法在 swift WebView 中加载/滚动完整的 pdf。
【发布时间】:2016-03-25 07:12:35
【问题描述】:

我正在尝试使用 swift 使用 Web 视图加载 pdf。它只能加载一页pdf,不能向下滚动超过一页。我能做什么?

导入 UIKit

类 ViewController: UIViewController,UIWebViewDelegate {

@IBOutlet var webViews: UIWebView!

var path = ""

override func viewDidLoad() {
    super.viewDidLoad()

    path  = NSBundle.mainBundle().pathForResource("ibook", ofType: "pdf")!
    let url = NSURL.fileURLWithPath(path)

    /*webViews.scalesPageToFit = true
    webViews.scrollView.scrollEnabled = true
    webViews.userInteractionEnabled = true*/

    webViews.delegate = self

    self.webViews.loadRequest(NSURLRequest(URL: url!
        ))
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}


func webViewDidStartLoad(webView : UIWebView) {
    //UIApplication.sharedApplication().networkActivityIndicatorVisible = true
    println("webViewDidStartLoad")
}

func webViewDidFinishLoad(webView : UIWebView) {
    //UIApplication.sharedApplication().networkActivityIndicatorVisible = [enter image description here][1]false
    webViews.scalesPageToFit = true
    webViews.scrollView.scrollEnabled = true
    webViews.userInteractionEnabled = true
    println("webViewDidFinishLoad")
}

}

【问题讨论】:

    标签: ios swift webview


    【解决方案1】:

    我在尝试显示外部 pdf(不是捆绑的)时遇到了类似的问题,但我想您可以使用相同的修复方法。

    在您的webViewDidFinishLoad 中,检查网址是否实际上是 pdf 网址。因为就我而言,我知道我在期待什么,所以我使用了简单的愚蠢检查。如果 url 链接到 pdf,您需要重新加载 web 视图以正确显示它并因此能够滚动。

    这里是 Objective C 中的一些简化代码。它在 Swift 中应该非常相似。试试这样的:

    - (void)webViewDidFinishLoad:(UIWebView *)webView {
        static BOOL isPdfReloaded = NO;
    
        if (!isPdfReloaded && [webView.request.URL.absoluteString containsString:@".pdf"])
        {
            [webView reload];
            isPdfReloaded = YES;
        }
        else
        {
            isPdfReloaded = NO;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多