ios 如何在可变高度 UITableView 内根据内容确定 UIWebView 高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/745160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to determine UIWebView height based on content, within a variable height UITableView?
提问by frankodwyer
I am trying to create a UITableView
with variable height rows as explained in the answer to this question
我正在尝试创建一个UITableView
高度可变的行,如此问题的答案中所述
My problem is each cell contains a UIWebView
with different (statically loaded) content I can't figure out how to calculate the proper height based on the content. Is there a way to do this? I've tried things like this:
我的问题是每个单元格都包含UIWebView
不同(静态加载)的内容,我无法弄清楚如何根据内容计算适当的高度。有没有办法做到这一点?我试过这样的事情:
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
WebViewCell *cell = (WebViewCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath];
[cell setNeedsLayout];
[cell layoutIfNeeded];
return cell.bounds.size.height;
}
The cells themselves are loaded from a nib, which is simply a UITableViewCell
containing a UIWebView
. (It would also be fine if the cells just adjusted themselves to the largest of the html content, though variable height would be nicer).
单元格本身是从笔尖加载的,笔尖只是一个UITableViewCell
包含UIWebView
. (如果单元格只是将自己调整为最大的 html 内容也可以,尽管可变高度会更好)。
回答by duncanwilcox
This code is probably too slow for table view use, but does the trick. It doesn't look like there's any alternative, as UIWebView offers no direct access to the DOM.
这段代码对于表视图的使用来说可能太慢了,但可以解决问题。看起来没有其他选择,因为 UIWebView 不提供对 DOM 的直接访问。
In a view controller's viewDidLoad
I load some HTML in a webview and when the load is finished run some javascript to return the element height.
在视图控制器中,viewDidLoad
我在 webview 中加载一些 HTML,加载完成后运行一些 javascript 以返回元素高度。
- (void)viewDidLoad
{
[super viewDidLoad];
webview.delegate = self;
[webview loadHTMLString:@"<div id='foo' style='background: red'>The quick brown fox jumped over the lazy dog.</div>" baseURL:nil];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *output = [webview stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"foo\").offsetHeight;"];
NSLog(@"height: %@", output);
}
回答by Unfalkster
I was very glad to find your answers, the javascript trick worked well for me.
我很高兴找到您的答案,javascript 技巧对我很有效。
However, I wanted to say that there is also the "sizeThatFits:" method :
但是,我想说还有“sizeThatFits:”方法:
CGSize goodSize = [webView sizeThatFits:CGSizeMake(anyWidth,anyHeigth)];
We have to set a preferred size different from zero, but apart from that, it usually always works !
我们必须设置一个不同于零的首选大小,但除此之外,它通常总是有效的!
Usually, because with the UIWebViews, it seems to work only on the second loading (I use the "loadHTMLString:" method, and yes I call "sizeThatFits:" from the delegate "didFinishLoad:" method).
通常,因为使用 UIWebViews,它似乎只适用于第二次加载(我使用“loadHTMLString:”方法,是的,我从委托“didFinishLoad:”方法调用“sizeThatFits:”)。
So, that's why I was very happy to find your solution which works in any case.
所以,这就是为什么我很高兴找到您在任何情况下都有效的解决方案。
回答by Dave Batton
This seems to work. For now.
这似乎有效。目前。
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGFloat webViewHeight = 0.0f;
if (self.subviews.count > 0) {
UIView *scrollerView = [self.subviews objectAtIndex:0];
if (scrollerView.subviews.count > 0) {
UIView *webDocView = scrollerView.subviews.lastObject;
if ([webDocView isKindOfClass:[NSClassFromString(@"UIWebDocumentView") class]])
webViewHeight = webDocView.frame.size.height;
}
}
}
It should safely return 0 if it fails.
如果失败,它应该安全地返回 0。
回答by Ortwin Gentz
The problem with -sizeThatFits:
is that it doesn't work out of the box, at least not on iOS 4.1. It just returns the current size of the webView (no matter whether it's called with CGSizeZero or an arbitrary non-zero size).
问题-sizeThatFits:
在于它不能开箱即用,至少在 iOS 4.1 上不能。它只返回 webView 的当前大小(无论是使用 CGSizeZero 调用还是任意非零大小调用)。
I found out that it actually works if you reduce the frame height of the webView prior to calling -sizeThatFits:
.
我发现如果在调用-sizeThatFits:
.
See my solution: How to determine the content size of a UIWebView?
请参阅我的解决方案:如何确定 UIWebView 的内容大小?
回答by Daij-Djan
measuring a webview works but only when it has loaded its content
测量 webview 有效,但只有当它加载了它的内容时
- making webview small (like 5px)
- load it and wait for finish
- calling sizeToFit on it then
- getting the size THEN
- reload the tableview (return the correct height for the cell)
- 使 webview 变小(比如 5px)
- 加载它并等待完成
- 然后调用 sizeToFit
- 得到尺寸 THEN
- 重新加载 tableview(返回正确的单元格高度)
advertisement: :D see my M42WebviewTableViewCell class on github which struggles with this
广告::D 在 github 上看到我的 M42WebviewTableViewCell 类,它与此斗争
回答by Ans
The better approach is to use scrollView.contentSize.height
property, like below.
更好的方法是使用scrollView.contentSize.height
属性,如下所示。
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(@"%f",webView.scrollView.contentSize.height);
}
回答by Kabhal
If you have bad values with sizeThatFits the first time but not after, here a possible cause I've encountered : a bug with external CSS. It seems webViewDidFinisLoad is called to soon, before external CSS are retrieved, and the web document fully built. I think the bug disappears if you reload because of the cache.
如果您第一次使用 sizeThatFits 的值不正确,但之后没有,这里可能是我遇到的一个原因:外部 CSS 的错误。似乎 webViewDidFinisLoad 在检索外部 CSS 之前很快就会被调用,并且 Web 文档已完全构建。我认为如果由于缓存而重新加载,该错误就会消失。
A trick : Preload your CSS with a dummy UIWebView soon in your application.
一个技巧:很快在你的应用程序中用一个虚拟的 UIWebView 预加载你的 CSS。
SDK : IOS4
SDK : IOS4