问题
I have a UIWebView and UIButton added as subview to my View. I also have a large webpage displayed inside my UIWebView, is it possible to scroll the webpage by clicking a button on the UIView?
I want to make the webpage scroll untill it reaches the bottom of the document.
The javascript to be used is : window.scrollTo(xPos, yPos);
Is it possible to make a javascript call on the button action which will scroll the web document inside the UIWebView?
回答1:
You can use that on your UIWebview component:
NSString *yourScript = [NSString stringWithFormat:@"javascript_method();"];
NSString* responseJS = [webView stringByEvaluatingJavaScriptFromString:yourScript];
回答2:
Scroll the UIWebView by calling JavaScript through Objective-C:
NSString *script = @"scrollTo(0, 0)";
[webView stringByEvaluatingJavaScriptFromString:script];
For smooth scrolling, use a library like jQuery:
NSString *script = @"$('html, body').animate({scrollTop:0}, 'slow')";
[webView stringByEvaluatingJavaScriptFromString:script];
回答3:
I had a similar requirement when I wanted to scroll the UIWebview Programmatically as I had no control over the web-pages I was loading. I wanted a way to do it in my ObjC code.
Solution:
UIWebview has is own UIScrollView we can play with it
1. Set the Edge inset for the _webview to whatever you want.
2. Then tell the _webview to scroll to the specified position.
[[_webView scrollView]setContentInset:UIEdgeInsetsMake(-300, 0, 0, 0)];
[[_webView scrollView]scrollRectToVisible:CGRectMake(0, -300, 768, 1024) animated:YES];
来源:https://stackoverflow.com/questions/1131859/scrolling-a-webpage-inside-uiwebview