【问题标题】:UIWebView document stays height of tallest contentUIWebView 文档保持最高内容的高度
【发布时间】:2011-06-02 17:33:55
【问题描述】:

我正在通过以下调用将内容加载到 UIWebView:

NSString *detailHtmlString = [NSString stringWithFormat:@"<html><head><style>body {background: black;color: white;line-height: 1.4em; font-family: sans-serif; margin: 0; padding: 0 0 30px 0;width: 280px;} p {margin-top: 10px;}</style></head><body id=\"content\">%@</body></html>",[currentWeek objectForKey:@"post_content"]];
[weekDetailController.movie_detail loadHTMLString:detailHtmlString baseURL:nil];

然后,在它完成加载之后:

- (void) webViewDidFinishLoad:(UIWebView *)sender {
    [self performSelector:@selector(calculateWebViewSize) withObject:nil afterDelay:0.1];
}

- (void) calculateWebViewSize {

    [weekDetailController.movie_detail sizeToFit];
    float newHeight = [[weekDetailController.movie_detail stringByEvaluatingJavaScriptFromString :@"document.getElementById(\"content\").offsetHeight"] floatValue];


    newHeight += 300; //height of the other elements
    NSLog(@"Height: %f",newHeight);

    CGRect frame = weekDetailController.movie_detail.frame;

    NSLog(@"Height was: %f",frame.size.height);

    frame.size.height = newHeight;

    weekDetailController.movie_detail.frame = frame;

    [scrollView.scrollView setContentOffset:CGPointZero];
    [scrollView.scrollView setContentSize:CGSizeMake(frame.size.width, frame.size.height)];
}

这在第一次加载视图时效果很好,但在后续视图中,当不同的内容已加载到 UIWebView 中(使用与上面相同的调用)时,获取高度的 JavaScript 调用返回越来越大的值 - - 即使内容更小,它仍然返回在某个时间点出现在视图中的最大内容的高度。我尝试将一个空白 HTML 字符串加载到视图中以“清除”它,但这似乎也无济于事。

任何提示将不胜感激!

【问题讨论】:

  • 嗯...您没有在上面的代码中重置 newHeight,只是在每次调用时添加 300...
  • 哎呀 - 删除注释代码并删除了设置 newHeight 的行(未注释) - 原始帖子已被修改

标签: iphone objective-c uiwebview uiscrollview


【解决方案1】:
[scrollView.scrollView setContentOffset:pt];
[scrollView.scrollView setContentSize:CGSizeMake(frame.size.width, frame.size.height)];

你在使用嵌套的 UIScrollViews 吗?可能是您只设置了内部滚动视图的大小,而将外部滚动视图保留为其内容的最大大小。

编辑:

你也应该使用[scrollView.scrollView setContentOffset:CGPointZero];

【讨论】:

  • 修复了 CGPointZero 项目——感谢您的提示。不,它不是一个嵌套的 UIScrollView——只是一个里面有一个 UIWebView 的。您粘贴的代码只是我上面的代码的副本,所以很遗憾,还没有解决任何问题。
  • 是的,我确实复制了,我的意思是指出我指的是您的代码的哪一部分。不过,我想知道你为什么使用scrollView.scrollView... 反正 UIWebView 是 UIScrollView 的子类,所以你还是应该设置外滚动视图的 contentSize。你在做吗?
  • 据我所知,UIWebView 不是 UIScrollView 的子类——它直接继承自 UIView,尽管它确实符合 UIScrollView 委托。它绝对没有 contentSize 元素——只有我在上面设置的 frame 属性。
  • 哦,是的,对不起,我的错。直到现在我还没有真正检查过 UIWebView 类参考。无论如何,我无法用这些代码进一步帮助你。能不能看看有没有其他相关的线路并贴出来?
【解决方案2】:

当使用带有约束的嵌套 UIWebViews 时,您需要重置约束(例如设置为 1)并在之后重新加载 webview:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, coordinator.transitionDuration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
    self.webViewHeightConstraint.constant = 1;
    [self reloadWebView]; //[self.webview reload] doesn't work here, simply load with the given url or given html content
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多