在scrollview.contentsize中,UIWebView内容高度不准确 [英] UIWebView content height not accurate in scrollview.contentsize

查看:927
本文介绍了在scrollview.contentsize中,UIWebView内容高度不准确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试滚动到我在UIWebView中加载的pdf文档中的特定页面。我可以在iOS 5中使用以下内容完成此操作:

I am trying to scroll to a particular page in a pdf document I have loaded in a UIWebView. I am able to do this without a problem in iOS 5 using:

pageHeight = webView.scrollView.contentSize.height / numberOfPages;
[[webView scrollView] setContentOffset:CGPointMake(0,pageNumber*pageHeight) animated:YES];

这只适用于页面已经加载到视图中并且滚动到的时间由用户操作。对于我想将新的pdf加载到webview到特定页面的情况,我遇到了麻烦。我正在请求加载新的pdf,然后在页面加载完成后执行以下操作:

This only works however if the page has been loaded into the view already and the scroll to is triggered by a user action. For the case where I want to load a new pdf into the webview to a specific page, I am having trouble. I am requesting the load of the new pdf, and then doing something like the following when the page has completed loading:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    int contentHeight = myWebView.scrollView.contentSize.height;
    int pageHeight = contentHeight / 5; // 5 total pages in example PDF
    int scrollDelta = pageHeight*2; // Scroll forward 2 pages

    [[myWebView scrollView] setContentOffset:CGPointMake(0,scrollDelta) animated:YES];
}

myWebView.scrollView.contentSize.height是我运行时的完整文档高度在加载初始文档后按钮上的类似代码,当我加载新文档并且webViewDidFinishLoad触发myWebView.scrollView.contentSize.height等于UIWebview的高度,而不是其中的内容。这当然会使pageHeight比它需要的小得多,并且不会滚动到正确的位置。

While myWebView.scrollView.contentSize.height is the full document height when I run similar code on a button click after the initial document loads, when I load a new document and webViewDidFinishLoad fires myWebView.scrollView.contentSize.height is equal to the height of the UIWebview, and not the content inside of it. This of course makes pageHeight much smaller than it needs to be and does not scroll to the correct position.

我尝试过像延迟滚动码一样的黑客攻击webViewDidFinishLoad认为可能页面还没有完全加载,即使我收到了回调,但在那些情况下,contentSize.height由于某种原因是0。

I've tried hacks like delaying the scroll code for a bit after webViewDidFinishLoad thinking maybe the page hasn't fully loaded even though I'm getting the callback, but in those cases contentSize.height is 0 for some reason.

任何想法都会非常感谢。

Any ideas would be greatly appreciated.

谢谢!

推荐答案

获取内容您可以执行以下操作来查看webview中包含的大小或页数:

to get the content size or the number of pages contained in the webview you may do the following:

    float w = [[mywebview stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].scrollWidth;"] floatValue];
    int pageCount = ceil(w/self.view.frame.size.width);

并滚动到网页浏览中的某个位置,您可以执行以下操作:

and to scroll to a certain position in your webview you may do the following:

    [mywebview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.scrollTo(%d,0);",xPosition]];

您觉得它很有用。

这篇关于在scrollview.contentsize中,UIWebView内容高度不准确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆