UIWebView在ScrollView中加载时内容发生变化

当用户阅读所选新闻时,我想制作一些类似Pulse的新闻。

未加载图像时,图像的空间尚未指定。 加载图像后,内容的大小也会发生变化(文本将被下推以为图像留出空间)。

我怎样才能做到这一点? 我现在所做的就是使用滚动视图,里面有uiwebview。

- (void)loadView { self.view = [[UIView alloc] initWithFrame:CGRectZero]; UIView *thisView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)]; contentTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 292, 82)]; contentThumbnailWebView = [[UIWebView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(contentTitleLabel.frame), 292, 0)]; contentDateLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(contentThumbnailWebView.frame), 292, 23)]; playVideoButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(contentThumbnailWebView.frame)/2, CGRectGetMaxY(contentThumbnailWebView.frame)/2, 72, 37)]; [playVideoButton setTitle:@"Play" forState:UIControlStateNormal]; playVideoButton.hidden = YES; contentSummaryLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(contentDateLabel.frame), 292, 20)]; contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)]; contentScrollView.backgroundColor = [UIColor whiteColor]; [thisView addSubview:contentScrollView]; [contentScrollView addSubview:contentDateLabel]; [contentScrollView addSubview:contentTitleLabel]; [contentScrollView addSubview:contentThumbnailWebView]; [contentScrollView addSubview:playVideoButton]; [contentScrollView addSubview:contentSummaryLabel]; //[self.view addSubview:thisView]; self.view = thisView; contentThumbnailWebView.delegate = self; } 

我已经阅读了几个关于这个主题,但我仍然无法解决它。

 - (void)webViewDidFinishLoad:(UIWebView *)aWebView { CGRect frame = aWebView.frame; frame.size.height = 1; aWebView.frame = frame; CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero]; fittingSize.width = aWebView.frame.size.width; frame.size = fittingSize; aWebView.frame = frame; NSLog(@"size: %f, %f", fittingSize.width, fittingSize.height); [contentDateLabel setFrame:CGRectMake(10, CGRectGetMaxY(aWebView.frame), 292, 23)]; playVideoButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(aWebView.frame)/2, CGRectGetMaxY(aWebView.frame)/2, 72, 37)]; [contentSummaryLabel setFrame:CGRectMake(10, CGRectGetMaxY(contentDateLabel.frame), 292, contentSummaryLabel.frame.size.height )]; contentScrollView.contentSize = CGSizeMake(contentScrollView.frame.size.width, CGRectGetMaxX(contentSummaryLabel.frame)); 

}

我加载jpg图像链接就可以了。 图像被加载,但高度不正确调整。 而且我也不能再次加载webview时滚动scrollView。

下面是当我调用图像被加载

 - (void) setImageAsThumbnail:(NSString *)imagehumbnailLink { NSURL *imageURL = [NSURL URLWithString:imageThumbnailLink]; NSString *cssString = @"<style type='text/css'>img {width: 292px; height: auto;}</style>"; NSString *myHTMLContent = @"<html><head></head><body><img src='%@'></body></html>"; NSString *htmlString = [NSString stringWithFormat:@"%@%@",cssString,myHTMLContent]; NSString *imageHTML = [[NSString alloc] initWithFormat:htmlString, imageURL]; contentThumbnailWebView.scalesPageToFit = YES; [contentThumbnailWebView loadHTMLString:imageHTML baseURL:nil]; } 

谢谢!

我经常在UIScrollView中使用UIWebView,而且因为加载HTML是asynchronous完成的,所以在完成时,必须调整Web视图和滚动视图的内容大小。

 [webView sizeThatFits:CGSizeZero] 

对我也不起作用。 相反,在您的webViewDidFinishLoad:(UIWebView*)webView您可以通过执行一段JavaScript来测量HTML文档的大小:

 NSString *js = @"document.getElementById('container').offsetHeight;"; NSString *heightString = [webView stringByEvaluatingJavaScriptFromString:js]; int height = [heightString intValue]; // HTML document height in pixels 

在这个例子中,您必须将ID“容器”分配给HTML中的顶级元素(确保其周围没有空白)。 这种方法对我来说效果很好。