Scrolling a webpage inside UIWebView

别来无恙 提交于 2019-12-04 06:21:14
Albrecht

You can use that on your UIWebview component:

NSString *yourScript = [NSString stringWithFormat:@"javascript_method();"]; 
NSString* responseJS = [webView stringByEvaluatingJavaScriptFromString:yourScript];

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];

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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!