简体   繁体   English

在UIWebView中滚动

[英]Scrolling in UIWebView

I want to know if it's possible to scroll to a given id in a UIWebView. 我想知道是否可以在UIWebView中滚动到给定的id。

I know that I can scroll to a given (x,y) point using: 我知道我可以使用以下方式滚动到给定的(x,y)点:

 [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: 我在本地加载html文件,所以我发现html文件中的元素可以“标记”如下:

 <element id="myID">

Instead of using a (x,y) coordinate to scroll, I want to scroll to a given id, I tried using this: 而不是使用(x,y)坐标滚动,我想滚动到给定的ID,我尝试使用这个:

 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. 提前致谢。

只需导航到这样的id:

[self.myWebView stringByEvaluatingJavaScriptFromString: @"window.location.hash='#myID'"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM