Reputation: 1413
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.
Upvotes: 3
Views: 1312
Reputation: 15070
Just navigate to the id like this :
[self.myWebView stringByEvaluatingJavaScriptFromString: @"window.location.hash='#myID'"];
Upvotes: 6