【问题标题】:Scroll to bottom of webview programmatically with swift使用 swift 以编程方式滚动到 webview 的底部
【发布时间】:2017-02-06 05:21:36
【问题描述】:

我有以下网页视图:

@IBOutlet weak var webView_MyContent: UIWebView!

并像这样加载自定义 html 内容:

self.webView_MyContent.loadHTMLString(html, baseURL: nil)

我想在我的内容加载时以编程方式滚动到页面的最底部。这将如何快速完成?

【问题讨论】:

    标签: ios swift xcode webview uiwebview


    【解决方案1】:

    您可以为此使用UIWebViewscrollView 属性。

    func webViewDidFinishLoad(_ webView: UIWebView) {
    
        let scrollPoint = CGPoint(x: 0, y: webView.scrollView.contentSize.height - webView.frame.size.height)
        webView.scrollView.setContentOffset(scrollPoint, animated: true)//Set false if you doesn't want animation
    }
    

    注意:不要忘记设置delegatewebView

    【讨论】:

    • @user2423476 欢迎朋友 :)
    【解决方案2】:

    不适用于 Swift 4 / WKWebView。

    相反,我找到了方法webView.scrollToEndOfDocument()

    加载完成后向下滚动,你可以把它放到这个函数中:

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        webView.scrollToEndOfDocument(self)
    }
    

    不要忘记导入 WebKit 并让你的类成为你的 WebView 的代表:

    // ...
    import WebKit
    // ...
    
    class ViewController: NSViewController, WKNavigationDelegate {
        // ...
    

    高级:完成 AJAX 请求后向下滚动

    现在,就我而言,我想向下滚动一个使用无限滚动的页面(当几乎到达页面末尾时,它开始加载其他内容)。

    这可以通过注入 JavaScript 并覆盖 XMLHttpRequest 方法来完成:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        // ...
    
        String javascript = String(contentsOfFile: Bundle.main.path(forResource: "script", ofType: "js"))
        webView.configuration.userContentController.add(self, name: "injectionHandler")
        webView.configuration.userContentController.addUserScript(WKUserScript.init(source: javascript, injectionTime: .atDocumentEnd, forMainFrameOnly: false))
    
        // ...
    
    }
    

    在你的 Xcode 项目中的文件 script.js 中,你将把这个:

    var open = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
        this.addEventListener("load", function() {
            var message = {"status": this.status, "requestUrl": url, "response": this.responseText, "responseURL": this.responseURL};
            webkit.messageHandlers.injectionHandler.postMessage(message);
        });
        open.apply(this, arguments);
    };
    

    要处理此事件(如果您还想捕获 AJAX 响应),您必须添加此方法:

    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        if message.name == "injectionHandler", let dict = message.body as? Dictionary<String, AnyObject>, let status = dict["status"] as? Int, let response = dict["response"] as? String {
            if status == 200 {
                webView.scrollToEndOfDocument()
            }
        }
    }
    

    并让你的类扩展 WKScriptMessageHandler:

    class ViewController: NSViewController, WKNavigationDelegate, WKScriptMessageHandler {
        // ...
    

    【讨论】:

    • 问题是关于iOS的。这个webView.scrollToEndOfDocument() 属于AppKit 框架,来自macOS 10.14+
    猜你喜欢
    • 2010-10-31
    • 2016-01-30
    • 2018-12-13
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 2015-07-21
    相关资源
    最近更新 更多