UICollectionView with UIWebView size issues

I need to implement a UICollectionView . The cells I use have UIWebViews inside them. The main problem is that I want the cell size to be adapted to the actual content inside the webViews.

I used the following two methods:

 - (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; } 

The problem, of course, is that webView only knows its size after loading it:

 - (void)webViewDidFinishLoad:(UIWebView *)webView 

How can I customize the UICollectionViewLayout size for a specific cell only after the UICollectionViewLayout loaded its contents?

Each example I found had either fixed sizes or didn't use web views that needed time until they calculated their size to fit their content.

Is there any way to do this?

+4
source share
2 answers

You can try this for the corresponding webView in webViewDidFinishLoad:

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

This will resize the webView to fit its content. Then compute the new layout and call invalidateLayout to use the new layout.

0
source

I’m stuck in a combination of WebView and CollectionView, and you are right, on the one hand, in the collection you need to determine the size of the cell, first of all, before displaying the collection itself. ie

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

where, on the other hand, WebView can determine the actual size, which is only required on webViewDidFinishLoad:

HOW TO SOLVE SUCH PROBLEMS?

In my case, I had only one section in which there was a webView. So what I did, I calculated the size in webViewDidFinishLoad: and then reloaded this particular section after it

 - (void)webViewDidFinishLoad:(UIWebView *)aWebView { if (!_isArticleLoaded) { CGRect frame = aWebView.frame; frame.size.height = 1; aWebView.frame = frame; CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero]; frame.size = fittingSize; aWebView.frame = frame; NSMutableIndexSet *mutableIndexSet = [[NSMutableIndexSet alloc] init]; [mutableIndexSet addIndex:2]; NSLog(@"size: %f, %f", fittingSize.width, fittingSize.height); _height = [NSNumber numberWithFloat:fittingSize.height]; _isArticleLoaded = YES; [_collectionView reloadSections:mutableIndexSet]; } } 

Therefore, initially in viewdidload: current values ​​are assigned to local variables:

 _isArticleLoaded = NO; _height = [NSNumber numberWithFloat:200.0]; 

and for UICollectionView I had the following: -

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 2) { return CGSizeMake(self.collectionView.frame.size.width,[_height floatValue]+35); } } 

This pretty much solved my problem when I was dealing with one section.

In your case, my friend, you should not use WebView for each cell, it will prove to be expensive

0
source

All Articles