【问题标题】:Resizing UIWebView height to fit content调整 UIWebView 高度以适应内容
【发布时间】:2012-01-08 14:47:45
【问题描述】:

我正在将任意长度的 HTML 字符串加载到 UIWebView 中,然后将其显示在 UITableViewCell 中。我不希望 UIWebView 独立于 UITableView 滚动,因此我必须调整其高度以适应内容。

这个UITableViewCell 也是可折叠的,当它处于折叠状态时不会显示UIWebView

问题是,我怎么知道这个高度是多少,这样我才能让heightForRowAtIndexPath 返回一个正确的值并允许表格正确查看和滚动?

下面是一些示例代码:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0)
    return 286;

if (indexPath.row == 1) {
    if (analysisExpanded)
        return analysisWebViewHeight + 39;
    else 
        return 52;
}

if (sourcesExpanded)
    return sourcesWebViewHeight + 39;

return 53;

}

很简单。 39 是我在带有 web 视图的单元格中的一些标题内容的高度。

我正在从 nib 加载单元格的“扩展视图”,因此要获取 web 视图,我调用 viewForTag:

        UIWebView* sourcesWebView = (UIWebView*)[cell viewWithTag:1];
        [sourcesWebView setBackgroundColor:[UIColor clearColor]];
        [sourcesWebView loadHTMLString:placeholder baseURL:[NSURL URLWithString:@"http://example.com/"]];
        sourcesWebViewHeight = [[sourcesWebView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"] floatValue];
        [sourcesWebView setFrame:CGRectMake(sourcesWebView.frame.origin.x, sourcesWebView.frame.origin.y, sourcesWebView.frame.size.width, sourcesWebViewHeight)];

sourcesWebViewHeight 是由cellForRowAtIndexPath 中的上述代码更新的iVar。如果用户点击单元格三次,展开-折叠-展开,并进行一点滚动,最终它会获得正确的高度。

我尝试使用仅限内存的UIWebView 对象“预加载”高度,但由于某种原因从未返回正确的数字。

【问题讨论】:

  • Apple(在 UIWebView 文档中)严格说不要在 UITableView 中使用 UIWebView。
  • 我们已经发布了几个应用程序,将网络视图作为单元格,没有任何问题
  • 同样问题的更多更好的答案也可以在 [这里][1] 中找到。 [1]:stackoverflow.com/questions/3936041/…

标签: iphone objective-c


【解决方案1】:

对于 iOS Safari,您需要使用以下行:

NSInteger height = [[sourcesWebView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] integerValue];

document.documentElement.scrollHeight 不适用于此浏览器。

为了确保一切正常,您需要在 web 视图完成加载后调用它 =)

更新:另一个选项,仅适用于 iOS5,他们已将 scrollView 属性添加到 UIWebView,您可以在其中获取 contentSize

【讨论】:

    【解决方案2】:

    问题是我试图在 web 视图有机会渲染它之前获取滚动大小。

    在实现 didFinishLoad 委托方法后,我原来的 javascript 可以正常工作以获取高度。

    【讨论】:

      【解决方案3】:

      由于webViewDelegate可以在HTML代码加载时获取webView的高度, tableview:heightForRowAtIndexPath: 方法将进入无限循环 试图检索位于 tableView Cell 中的 webView:

      - (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
          float height;
      
          UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
          UIWebView *webContent = (UIWebView *) [cell viewWithTag:tagTableSingleContentWeb];
          CGRect frame = webContent.frame;
      
          height = frame.origin.y + frame.size.height + 30;
      
          return height;
      }
      

      以上是我在tableView单元格内的webView中插入HTML代码时尝试更改tableView单元格的代码,但它会导致无限循环......

      我还在寻找其他方法。

      附言WebViewDelegate webView 位于 ScrollView 中的方式是可以的,但我还不知道如何让它在 TableView 单元格中工作。

      【讨论】:

      • 您找到解决方案了吗?我也有同样的问题?
      【解决方案4】:

      here 也可以找到几乎相同问题的更多更好的答案。只是觉得它可能对某人有帮助。

      【讨论】: