单击链接上的缩放网页

时间:2015-10-08 15:49:23

标签: ios swift webview segue scale

所以我创建了一个应用程序,可以打开一个带有一些文本和链接的HTML页面。但是,如果我点击一个链接,我打开的页面将无法扩展(显然)。

我知道我可以在第一个ViewController中扩展WebView,但在这种情况下,我很难读取我的初始HTML页面。

我尝试了几种方法:

  1. 在点击的链接上缩放我的webView:

    if navigationType == UIWebViewNavigationType.LinkClicked {
    
        UIApplication.sharedApplication().openURL(request.URL!)
        myWebView.frame = UIScreen.mainScreen().bounds
        myWebView.center = self.view.center
        myWebView.scalesPageToFit = true
    }
    return true
    
  2. 或者那样:

    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    
        switch navigationType {
        case .LinkClicked:
            // Open links in Safari
    
            UIApplication.sharedApplication().openURL(request.URL!)
            myWebView.scalesPageToFit = true
            return false
        default:
            // Handle other navigation types...
            return true
        }
    }
    

    但是没有succsess。

    之后我尝试设置一个segue到我的第二个ViewController,以防点击链接但结果仍然相同。

    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        if navigationType == UIWebViewNavigationType.LinkClicked {
    
            let about = self.storyboard?.instantiateViewControllerWithIdentifier("openlink") as! dossierLink
            self.navigationController?.pushViewController(about, animated: true)
        }
        return true
    }
    

    有人可以帮助我吗?谢谢!

1 个答案:

答案 0 :(得分:0)

我的建议是只加载下一个html页面或网址

的内容

webView.loadDataWithBaseURL(null,urlcontent,“UTF-8”,null)

有很多方法可以将网址转换为字符串内容,例如Android Read contents of a URL (content missing after in result)