How to set cell cell width for dynamic stretching across the width of the phone

How can I adjust the view of the collection cell to dynamically stretch to the width of the iphone screen (e.g. iphone 5s, iphone 6 plus)?

I tried:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { (ResultCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellClass forIndexPath:indexPath]; cell.bounds = CGRectMake(0,0, self.view.bounds.size.width, 150); return cell; } 

This does not work. I do not see the content being stretched on the right side of the screen.

I tried to add this delegate method:

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { CGSize cellSize; cellSize.width = self.view.bounds.size.width; // body view height cellSize.height = 150; return cellSize; } 

I set breakpoints in a method, but this method is never called?

+8
ios objective-c
source share
2 answers

I had the same issue with the same use case. I finally finished work

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(collectionView.bounds.size.width, 150); } 

This changes the size of each element (cell) of the collection view. Therefore, you can resize each cell using this. Here I need the cell width to be the same as in my UICollectionView, so I passed the width of the UICollectionview and the specific height I wanted. Hope this helps.

There is also no need to set the cell borders in - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath , because the size is set via - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

Also make sure you bind the desired Delegates & DataSources to the UICollectionView

+13
source share

Cell size is determined by the collection layout object. Are you UICollectionViewFlowLayout ? Try either setting the itemSize property of the layout object or implementing the delegation method: collectionView:layout:sizeForItemAtIndexPath:

You can specify the size of the element (cell) relative to the boundaries of the collection view.

Keep in mind that you also need to customize the collection view so that it changes using your view controller. If you are using a UICollectionViewController that should be configured automatically. Otherwise, you will have to restrain viewing the collection to the limits of your supervisor.

Update:

You can set the itemSize property of the layout directly to the layout:

 self.layout.itemSize = CGSizeMake(CGRectGetWidth(self.collectionView.bounds), 100); 

or (better if you are handling rotation or resizing), make the appropriate delegate call:

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(CGRectGetWidth(collectionView.bounds), 100); } 

I would recommend reading the relevant UICollectionView to learn more about how collection views and their layout work:

+1
source share

All Articles