Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize UICollectionView cells after their data has been set

My UICollectionView cells contain UILabels with multiline text. I don't know the height of the cells until the text has been set on the label.

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

This was the initial method I looked at to size the cells. However, this is called BEFORE the cells are created out of the storyboard.

Is there a way to layout the collection and size the cells AFTER they have been rendered, and I know the actual size of the cell?

like image 478
Shocks Avatar asked Dec 06 '12 18:12

Shocks


People also ask

How do I resize a collection view cell?

First you have to Add the UICollectionViewDelegateFlowLayout delegate and then use following delegate method it will work fine for me. Show activity on this post. Implement the below method,, it is autoresized base on the screen you have.

How do I increase the collection height of a cell in Swift?

Try to use UICollectionViewDelegateFlowLayout method. In Xcode 11 or later, you need to set Estimate Size to none from storyboard. Show activity on this post. You can put the width and height you want in the CGSize constructor.

How does Uicollectionview work?

The collection view presents items onscreen using a cell, which is an instance of the UICollectionViewCell class that your data source configures and provides. In addition to its cells, a collection view can present data using other types of views.


1 Answers

I think your are looking for the invalidateLayout method you can call on the .collectionViewLayout property of your UICollectionView. This method regenerates your layout, which in your case means also calling -collectionView: layout: sizeForItemAtIndexPath:, which is the right place to reflect your desired item size. Jirune points the right direction on how to calculate them.

An example for the usage of invalidateLayout can be found here. Also consult the UICollectionViewLayout documentation on that method:

Invalidates the current layout and triggers a layout update.

Discussion:

You can call this method at any time to update the layout information. This method invalidates the layout of the collection view itself and returns right away. Thus, you can call this method multiple times from the same block of code without triggering multiple layout updates. The actual layout update occurs during the next view layout update cycle.

Edit:

For storyboard collection view which contains auto layout constraints, you need to override viewDidLayoutSubviews method of UIViewController and call invalidateLayout collection view layout in this method.

- (void)viewDidLayoutSubviews {     [super viewDidLayoutSubviews];     [yourCollectionView.collectionViewLayout invalidateLayout]; } 
like image 65
Jan Z. Avatar answered Sep 19 '22 22:09

Jan Z.