Auto scroll bottom of page uiwebview
I wish to once the webview loads
- (void)webViewDidFinishLoad:(UIWebView *)webBrowser {
//Scroll to bottom
}
Just focus on the bottom so basically just show 开发者_如何学Pythonto bottom of the uiwebview and maybe lock it there so user can't scroll.
First you would have to get the content height, i have never done this personally, but here is a good place to start:
Here are some constants available that give the document area of the window that is available for writing to. These will not be available until after the document has loaded and the method used for referencing them is browser specific. The available constants are:
window.innerHeight/Width Provided by most browsers, but importantly, not Internet Explorer. document.body.clientHeight/Width Provided by many browsers, including Internet Explorer. document.documentElement. clientHeight/Width Provided by most DOM browsers, including Internet Explorer.
Then to scroll to the offset you want is done like this:
[webView stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat:@"scrollTo(0,%d)",scrollPosition];
Operations inside a webview are controlled with JavaScript, so you'll find various solutions at Scroll Automatically to the Bottom of the Page.
Example based on Tho answer (var scrollingElement = (document.scrollingElement || document.body); scrollingElement.scrollTop = scrollingElement.scrollHeight;
):
With UIWebView (deprecated)
DispatchQueue.global().async {
_ = webView.stringByEvaluatingJavaScript(from: "var scrollingElement = (document.scrollingElement || document.body); scrollingElement.scrollTop = scrollingElement.scrollHeight;")
}
With WKWebView (iOS 8+)
webView.evaluateJavaScript("var scrollingElement = (document.scrollingElement || document.body); scrollingElement.scrollTop = scrollingElement.scrollHeight;")
精彩评论