ios - WKWebView 没有将内容滚动到映射的 HTML

标签 ios swift wkwebview xcode10 wknavigationdelegate

我正在研究 WKWebView 以在 WKWebView 中呈现 HTML。

My HTML has <a href ='#bottom'>

这个 anchor 标记会将我的 WKWebView 推送到映射到它的内容。 我的完整 HTML 是:

<!DOCTYPE html><html lang="en"><head> <title>Hello</title><style>body{ font-size: 40px;}</style> </head><body><a id='LnkTop'></a><p><a href='#bottom'> Link 1</a></p><div><h3><a id='top'> Link 1 </a>click will bring cursor here..</h3><p>HTML offers many of the conventional publishing idioms for rich text and structured documents, but what separates it from most other markup languages is its featuresfor hypertext and interactive documents. This section introduces the link (or hyperlink, or Web link), the basic hypertext construct.A link is a connection from one Web resource to another. Although a simple concept, the link has been one of the primary forces driving the success of the Web.A link has two ends -- called anchors -- and a direction. The link starts at the 'source' anchor and points to the 'destination' anchor,which may be any Web resource (e.g., an image, a video clip, a sound bite, a program, an HTML document, an element within an HTML document, etc.).</p></div><div id='bottom'><h3>Link 2 click will bring cursor here..</h3><p>HTML offers many of the conventional publishing idioms for rich text and structured documents, but what separates it from most other markup languages is its featuresfor hypertext and interactive documents. This section introduces the link (or hyperlink, or Web link), the basic hypertext construct.A link is a connection from one Web resource to another. Although a simple concept, the link has been one of the primary forces driving the success of the Web.A link has two ends -- called anchors -- and a direction. The link starts at the 'source' anchor and points to the 'destination' anchor,which may be any Web resource (e.g., an image, a video clip, a sound bite, a program, an HTML document, an element within an HTML document, etc.).</p></div></body></html>

我处理链接按钮的代码如下:

func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
        print("Strat to load")

    }
    func webView(_ webView: WKWebView, shouldPreviewElement elementInfo: WKPreviewElementInfo) -> Bool {
        print("Raghav")
        return true
    }
    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
        print(error.localizedDescription)
    }
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        print("Strat to load")
    }
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print("finish to load")
        webviewReportNotes.evaluateJavaScript("window.scrollTo(0,0)", completionHandler: nil)
    }

    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        print("error ")
        print(error)
    }

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        print("clicked")
        if navigationAction.navigationType == WKNavigationType.linkActivated  {
            print("cancel")
            ///webviewReportNotes.evaluateJavaScript("window.scrollTo(0,0)", completionHandler: nil)
            decisionHandler(.cancel)
            self.webviewReportNotes.allowsLinkPreview = true
           // webView.load(navigationAction.request)
        }
        else
        {
            print("allow")
            decisionHandler(.allow)
        }
    }

    func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
        print("comitted")
    }
    func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
        print("entered")
        if navigationAction.targetFrame == nil {
            webView.load(navigationAction.request)
        }
        return nil

    }

当点击 anchor 标签时,它应该将 WKWebview 滚动到以 HTML 格式映射到它的内容,但这并没有发生。

I am using Xcode 10 and working on iOS 11

我不知道缺少什么我已经尝试了几乎所有但它没有滚动到 anchor 标记中的映射数据以查看 HTML,您可以将我的 HTML 粘贴到

https://codebeautify.org/htmlviewer/

请检查并让我知道我遗漏了什么。

提前致谢。 :)

最佳答案

  1. 确保您的 HTML 验证 - 您对 <p> 的使用标签无效(标题 <h3> 不能驻留在 p 标签中,例如); <title></title>是必需的
  2. name现在已过时,请使用 id属性而不是最近的容器,例如在封闭的div元素

import UIKit
import WebKit

class ViewController: UIViewController {
    let webView = WKWebView()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(webView)
        let html = "<!DOCTYPE html><html xmlns='http://www.w3.org/1999/xhtml'><head> <title>Hello</title><style>body{ font-size: 40px;}</style> </head><body><a name='LnkTop'></a><p><a href='#bottom'> Link 1</a></p><div>                                <h3><a name='top'> Link 1 </a>click will bring cursor here..                                </h3><p>HTML offers many of the conventional publishing idioms for rich text and structured documents, but what separates it from most other markup languages is its features                                for hypertext and interactive documents. This section introduces the link (or hyperlink, or Web link), the basic hypertext construct.A link is a connection from one Web resource to another. Although a simple concept, the link has been one of the primary forces driving the success of the Web.A link has two ends -- called anchors -- and a direction. The link starts at the 'source' anchor and points to the 'destination' anchor,which may be any Web resource (e.g., an image, a video clip, a sound bite, a program, an HTML document, an element within an HTML document, etc.).</p></div><div id=\"bottom\"><h3>Link 2 click will bring cursor here..</h3><p>HTML offers many of the conventional publishing idioms for rich text and structured documents, but what separates it from most other markup languages is its featuresfor hypertext and interactive documents. This section introduces the link (or hyperlink, or Web link), the basic hypertext construct.A link is a connection from one Web resource to another. Although a simple concept, the link has been one of the primary forces driving the success of the Web.A link has two ends -- called anchors -- and a direction. The link starts at the 'source' anchor and points to the 'destination' anchor,which may be any Web resource (e.g., an image, a video clip, a sound bite, a program, an HTML document, an element within an HTML document, etc.).</p></div></body></html>"
        webView.loadHTMLString(html, baseURL: nil)
        webView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
        webView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
        webView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
        webView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
        webView.translatesAutoresizingMaskIntoConstraints = false
    }
}

enter image description here

关于ios - WKWebView 没有将内容滚动到映射的 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53278456/

相关文章:

ios - SonarQube 7.4 使用 faSTLane 扫描 ObjectiveC

swift - 为什么在 Swift Playground 中执行的结果与在应用程序中执行的结果不同?

swift - Smashtag(Stanford CS193p Twitter 客户端)未在 iPhone 上加载

ios - 在 web View 中拦截加载的 javascript 文件

ios - 相当于WKWebView上UIWebView的paginationMode

ios - 折叠键盘、工具栏

ios - 在后台以编程方式在 iOS 中实现多个网络调用

ios - WKWebview 上的 ApplePay

ios - 涉及标准 iOS UIImagePickerController 的应用程序中的地理标记

swift - 将地址从一个 Vc 传递到另一个 Vc