简体   繁体   English

UIWebView内容高度不正确

[英]UIWebView incorrect content height

I'm trying to resize a UIWebView so that it fits the entire contents of the loaded page. 我正在尝试调整UIWebView大小,以使其适合已加载页面的全部内容。 To do this, I get the content height of the webview's scrollview, and set the height of the webview to that value. 为此,我获取了webview滚动视图的内容高度,并将webview的高度设置为该值。

For anything with content height over 200, this works just fine, and the view scales to exactly fit the page without any extra space. 对于内容高度超过200的任何内容,这都可以正常工作,并且视图可以缩放以完全适合页面,而无需任何额外空间。 But with content of height under 200, the webview seems to hit some sort of height floor, and webView.scrollView.contentSize.height won't return any values under 200. I'm left with a ton of whitespace. 但是,如果高度的内容小于200,则webview似乎达到了某种高度限制,并且webView.scrollView.contentSize.height不会返回任何小于200的值。我留下了大量的空格。

There are a number of posts on this topic, but none seem to cover this particular issue. 关于此主题的文章很多,但似乎都没有涵盖这个特定问题。 What is the best way to return the actual content height when that value is under 200? 当该值小于200时,返回实际内容高度的最佳方法是什么?

EDIT: Here's an example of HTML that should render quite short, but ends up being 200pt tall. 编辑:这是HTML的示例,该HTML应该呈现得很短,但最终却高200pt。

<meta name='viewport' content='width=device-width, user-scalable=yes'>
<html>
<head></head>
<body style='font-family:Helvetica neue; font-size:1.0em; color:#181818; -webkit-font-smoothing: antialiased;'><div>
Here's some example content.
<br/></div>
</body>

Please make sure that both your HTML and BODY tags do not have a min-height or height css styles. 请确保您的HTMLBODY标签都没有 min-heightheight css样式。 Then, you should take the height using JavaScript. 然后,您应该使用JavaScript来提高高度。 To get the actual height, instead of the scrollable size which is some kind of unknown UIWebView decision. 要获取实际高度,而不是滚动尺寸,这是某种未知的UIWebView决定。

So [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.offsetHeight"] floatValue] would do. 因此[[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.offsetHeight"] floatValue]可以做到。

I was having a similar problem where I was using a javascript call to get the content height and setting the web view's height to that value, but 568 seemed to be the minimum that those methods would return to me. 我遇到了类似的问题,当时我在使用javascript调用来获取内容高度并将Web视图的高度设置为该值,但是568似乎是这些方法返回给我的最小值。 I ended up having to first set my web view's height to 1, and then set the height to the result of the javascript calls. 我最终不得不首先将Web视图的高度设置为1,然后将高度设置为javascript调用的结果。

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

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