简体   繁体   English

如何在uiwebview上设置滚动位置

[英]How to set scroll position on uiwebview

I want to go to specify line on my uiwebview loaded. 我想去加载我的uiwebview上的行。 I tried 我试过了

[webView stringByEvaluatingJavaScriptFromString:@"window.scrollTo(0.0, 100.0)"]; [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 . 我只是想知道它,因为我在UIView加载了UIWebview Check my code below on my UIView : 在我的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 : 我把滚动定位脚本放在我的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. 在这种情况下,我想在加载视图时将我的uiwebview放到页面底部。 But it's totally not working, why? 但它完全不起作用,为什么? Am I doing it wrong? 我做错了吗?

尝试

webview.scrollView.contentOffset = CGPointMake(0, 100);

Make sure you set the view controller where you have your web view to be a UIWebViewDelegate , and declare this in the @interface of the header file (.h). 确保将视图控制器设置为UIWebViewDelegate ,并在头文件(.h)的@interface中声明。

[yourWebView setDelegate:self];



Then, in the webViewDidFinishLoad: method, insert this code: 然后,在webViewDidFinishLoad:方法中,插入以下代码:

[yourWebView.scrollView scrollRectToVisible:CGRectMake(0, yourWebView.bounds.size.height + 100, 1, 1) animated:NO];           

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

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