2011-11-11 8 views
0

ユーザーが選択したニュースを読むときにPulseニュースのようにしたいと思います。UIWebViewがScrollView内で読み込まれたときのコンテンツの変更

イメージがロードされていない場合、イメージのスペースはまだ指定されていません。イメージがロードされた後、コンテンツのサイズも変更されます(テキストがプッシュダウンされてイメージのスペースが確保されます)。

どうすればいいですか? 私は現在、スクロールビューを使用しており、内部に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];   
} 

をロードするために呼び出すときです!

答えて

0

UIWebViewをUIScrollView内で頻繁に使用します.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 

を、あなたはあなたのHTML内のトップレベルの要素にID「コンテナ」を割り当てる必要があります(周囲に余白がないことを確認してください)。このアプローチは私にとって非常にうまく機能します。

関連する問題