Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize the collection view cells according to device screen size?

I noticed that the cells would always follow the definition in size inspector. Even if I have already applied UICollectionViewDelegateFlowLayout.

enter image description here

enter image description here

So that's how it looks. But I want the cells to rather look like how it was on a smaller iphone screen.

enter image description here

like image 339
Chris Mikkelsen Avatar asked Dec 05 '16 13:12

Chris Mikkelsen


People also ask

How to resize 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.

What is Uicollectionviewflowlayout?

A layout object that organizes items into a grid with optional header and footer views for each section.


1 Answers

Implement sizeForItemAt method according to the view's frame size

Swift

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {      let height = view.frame.size.height     let width = view.frame.size.width     // in case you you want the cell to be 40% of your controllers view     return CGSize(width: width * 0.4, height: height * 0.4) } 

Objective C

- (CGSize)collectionView:(UICollectionView *)collectionView                layout:(UICollectionViewLayout *)collectionViewLayout     sizeForItemAtIndexPath:(NSIndexPath *)indexPath {     CGFloat height = self.view.frame.size.height;    CGFloat width  = self.view.frame.size.width;    // in case you you want the cell to be 40% of your controllers view    return CGSizeMake(width * 0.4, height * 0.4);  } 
like image 129
Marat Ibragimov Avatar answered Sep 19 '22 21:09

Marat Ibragimov