Scrolling webpage inside UIWebView

I have UIWebView and UIButton added as a subview for my view. I also have a large webpage displayed inside my UIWebView , is it possible to scroll a webpage by clicking a button on a UIView ?

I want to scroll the webpage until it reaches the bottom of the document.

Used javascript: window.scrollTo(xPos, yPos);

Is it possible to make a javascript call to a button action that will scroll a web document inside a UIWebView ?

+6
javascript objective-c iphone uiwebview
source share
3 answers

You can use this in your UIWebview component:

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

Scroll through the UIWebView by calling JavaScript via Objective-C:

 NSString *script = @"scrollTo(0, 0)"; [webView stringByEvaluatingJavaScriptFromString:script]; 

For smooth scrolling, use the jQuery library:

 NSString *script = @"$('html, body').animate({scrollTop:0}, 'slow')"; [webView stringByEvaluatingJavaScriptFromString:script]; 
+1
source share

I had a similar requirement when I wanted to scroll through the UIWebview Programmatically, since I did not have control over the web pages I loaded. I need a way to do this in ObjC code.

Solution: UIWebview has its own UIScrollView , we can play with it 1. Set the Edge tab for _webview as you like. 2. Then tell _webview scroll to the specified position.

 [[_webView scrollView]setContentInset:UIEdgeInsetsMake(-300, 0, 0, 0)]; [[_webView scrollView]scrollRectToVisible:CGRectMake(0, -300, 768, 1024) animated:YES]; 
0
source share

All Articles