gpt4 book ai didi

ios - UICollectionView 与 UIWebView 尺寸问题

转载 作者:可可西里 更新时间:2023-11-01 06:16:04 26 4
gpt4 key购买 nike

我需要实现一个UICollectionView。我使用的单元格中有 UIWebViews。主要问题是我想让单元格大小适应 webView 中的实际内容。

我使用了以下两种方法:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

CollectionViewCellDefault *cell = (CollectionViewCellDefault *)[collectionView cellForItemAtIndexPath:indexPath];

NSLog(@"rect %@", NSStringFromCGRect(cell.wvDisplayValue.frame));
CGSize retval = cell.wvDisplayValue.frame.size;
retval.height += 135;
retval.width += 135;
return retval;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCellDefault *cell = [collectionView dequeueReusableCellWithReuseIdentifier:detailColumnsCell forIndexPath:indexPath];

NSURL *indexFileURL = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
[cell.wvDisplayValue loadRequest:[NSURLRequest requestWithURL:indexFileURL]];
cell.lblTitle.text = @"Description: ";
cell.frame = cell.wvDisplayValue.frame;
return cell;

}

问题当然是 webView 只有在加载后才知道它的大小:

- (void)webViewDidFinishLoad:(UIWebView *)webView 

是否可以仅在 webview 加载其内容后调整特定单元格的 UICollectionViewLayout 的大小?

我发现的每个示例要么有固定大小,要么没有使用 webView,这需要时间才能计算出适合其内容的大小。

有没有办法做到这一点?

最佳答案

您可以针对 webViewDidFinishLoad 中的相应 webView 尝试此操作:

CGRect frame = webView.frame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
webView.frame = frame;

这将调整 webView 的大小以适应其内容。然后计算新布局,调用invalidateLayout使用新布局。

关于ios - UICollectionView 与 UIWebView 尺寸问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15063966/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com