Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set scroll position on uiwebview

I want to go to specify line on my uiwebview loaded. I tried

[webView stringByEvaluatingJavaScriptFromString:@"window.scrollTo(0.0, 100.0)"];

but it's not working. My webview still start with top of the page. I just wonder its because I load the UIWebview inside UIView. Check my code below on my 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    
}

and I put the scroll positioning script on my 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];
}

in this case I want to put my uiwebview go to the bottom of the page when the view is loaded. But it's totally not working, why? Am I doing it wrong?

like image 441
dejoong Avatar asked Dec 12 '11 07:12

dejoong


1 Answers

Try

webview.scrollView.contentOffset = CGPointMake(0, 100);
like image 161
Dani Avatar answered Oct 14 '22 22:10

Dani