Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrolling in UIWebView

I want to know if it's possible to scroll to a given id in a UIWebView.

I know that I can scroll to a given (x,y) point using:

 [self.myWebView.scrollView setContentOffset:CGPointMake(x, y) animated:YES];

I'm loading html files locally so I found that elements in html files can be "labeled" as follows:

 <element id="myID">

Instead of using a (x,y) coordinate to scroll, I want to scroll to a given id, I tried using this:

 NSString *yCoordinate = [self.myWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('myID').value"];
 [self.myWebView.scrollView setContentOffset:CGPointMake(0, [yCoordinate intValue]) animated:YES];

But it's not working. What am I doing wrong?

Thanks in advance.

like image 854
Edu Avatar asked Dec 04 '25 02:12

Edu


1 Answers

Just navigate to the id like this :

[self.myWebView stringByEvaluatingJavaScriptFromString: @"window.location.hash='#myID'"];
like image 164
Zakaria Avatar answered Dec 05 '25 15:12

Zakaria



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!