使用 swift 以编程方式滚动到 webview 的底部
Scroll to bottom of webview programmatically with swift
我有以下网络视图:
@IBOutlet weak var webView_MyContent: UIWebView!
并像这样加载自定义 html 内容:
self.webView_MyContent.loadHTMLString(html, baseURL: nil)
我想在加载内容时以编程方式滚动到页面底部。这将如何在 swift 中完成?
您可以使用 UIWebView
中的 scrollView
属性。
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
}
注意:不要忘记设置 webView
的 delegate
。
Swift 4 / WKWebView 对我不起作用。
相反,我找到了方法 webView.scrollToEndOfDocument()
要在加载完成后向下滚动,可以将其放入此函数中:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.scrollToEndOfDocument(self)
}
不要忘记导入 WebKit 并使您的 class 成为 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()
}
}
}
并使您的 class 扩展 WKScriptMessageHandler:
class ViewController: NSViewController, WKNavigationDelegate, WKScriptMessageHandler {
// ...
我有以下网络视图:
@IBOutlet weak var webView_MyContent: UIWebView!
并像这样加载自定义 html 内容:
self.webView_MyContent.loadHTMLString(html, baseURL: nil)
我想在加载内容时以编程方式滚动到页面底部。这将如何在 swift 中完成?
您可以使用 UIWebView
中的 scrollView
属性。
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
}
注意:不要忘记设置 webView
的 delegate
。
Swift 4 / WKWebView 对我不起作用。
相反,我找到了方法 webView.scrollToEndOfDocument()
要在加载完成后向下滚动,可以将其放入此函数中:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.scrollToEndOfDocument(self)
}
不要忘记导入 WebKit 并使您的 class 成为 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()
}
}
}
并使您的 class 扩展 WKScriptMessageHandler:
class ViewController: NSViewController, WKNavigationDelegate, WKScriptMessageHandler {
// ...