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

时间:2011-11-11 05:18:14

标签: objective-c ios uiwebview uiscrollview

当用户阅读所选新闻时,我想制作类似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];         
}

谢谢!

1 个答案:

答案 0 :(得分:0)

我经常在UIScrollView中使用UIWebView,因为加载HTML是异步完成的,所以你必须在完成后调整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中的顶级元素(确保它周围没有边距)。这种方法对我很有效。