[英]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.