[英]How to set scroll position on uiwebview
我想去加载我的uiwebview上的行。 我试过了
[webView stringByEvaluatingJavaScriptFromString:@“window.scrollTo(0.0,100.0)”];
但它不起作用。 我的网页浏览仍然从页面顶部开始。 我只是想知道它,因为我在UIView
加载了UIWebview
。 在我的UIView
上查看下面的代码:
- (void)loadView {
webview = [[WebViewDetail alloc]initWithFrame:[UIScreen mainScreen].applicationFrame]; //initialize a mainView is a UIWebVIew
webview.managedObjectContext = self.managedObjectContext;
webview.kitab = self.kitab;
webview.currentPasal = self.pasal;
self.title = [NSString stringWithFormat:@"%@ %d", self.kitab, webview.currentPasal];
self.view=webview; //make the mainView as the view of this controller
}
我把滚动定位脚本放在我的UIWebView
:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
int height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] intValue];
NSString* javascript = [NSString stringWithFormat:@"window.scrollBy(0, %d);", height];
[self stringByEvaluatingJavaScriptFromString:javascript];
}
在这种情况下,我想在加载视图时将我的uiwebview放到页面底部。 但它完全不起作用,为什么? 我做错了吗?
尝试
webview.scrollView.contentOffset = CGPointMake(0, 100);
确保将视图控制器设置为UIWebViewDelegate
,并在头文件(.h)的@interface
中声明。
[yourWebView setDelegate:self];
然后,在webViewDidFinishLoad:
方法中,插入以下代码:
[yourWebView.scrollView scrollRectToVisible:CGRectMake(0, yourWebView.bounds.size.height + 100, 1, 1) animated:NO];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.