Rayen Kamta
Rayen Kamta

Reputation: 214

Resizing Collection View Cells

I have 2 collections views on one view controller. I want the cells to resize height & width to the Height of the collection view.

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  float cellHeight = 0;
  float cellWidth = 0;
     if (collectionView == self.channelsCollectionView) {

cellHeight = self.channelsCollectionView.collectionViewLayout.collectionViewContentSize.height;
cellWidth = cellHeight ;
 }
  if (collectionView == self.favoritesCollectionView){
cellHeight = self.favoritesCollectionView.collectionViewLayout.collectionViewContentSize    .height;
cellWidth = cellHeight ;

 }

 return CGSizeMake(cellHeight, cellWidth);
}

Upvotes: 0

Views: 330

Answers (2)

bhumika
bhumika

Reputation: 13

You can manage this by giving collection view tag

 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
    sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
 {
    //here You have to check collection view tag
      if(collectionView.tag == 1)
      {
          //define first collection view height and width
      }
      else if(collectionView.tag == 2)
      {
          //define second collection view height and width
      }
 }

Upvotes: 0

Tim007
Tim007

Reputation: 2557

Try this one

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  float cellHeight = 0;
  float cellWidth = 0;
     if (collectionView == self.channelsCollectionView) {

cellHeight = self.channelsCollectionView.frame.size.height;
cellWidth = cellHeight ;
 }
  if (collectionView == self.favoritesCollectionView){
cellHeight = self.favoritesCollectionView.frame.size.height;
cellWidth = cellHeight ;

 }

 return CGSizeMake(cellHeight, cellWidth);
}

or

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  float cellHeight = collectionView.frame.size.height;
  return CGSizeMake(cellHeight, cellHeight);
}

Upvotes: 1

Related Questions