Pratik Panchal
Pratik Panchal

Reputation: 529

How to Display 3*3 CollectionView Cell For Dynamic Height in Swift

I want to show 3*3 Collection View Dynamic Height cell any one known how to display this

thanks in Advance.!!!

Upvotes: 0

Views: 487

Answers (1)

AnthoPak
AnthoPak

Reputation: 4391

Here's what you can use to have 3 * 3 Collection View regarding iPhone screen size (assuming that your collectionView frame is set according to screen size) :

Swift

optional func collectionView(collectionView: UICollectionView,
                               layout collectionViewLayout: UICollectionViewLayout,
                                      sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    var spaceBetweenCells = 12
    var width = (self.collectionView.frame.size.width - spaceBetweenCells * 3) / 3
    var height = (self.collectionView.frame.size.height - spaceBetweenCells * 3) / 3
    return CGSizeMake(width, height)
}

Objective C

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{    
    CGFloat spaceBetweenCells = 12;
    CGFloat width = (self.collectionView.frame.size.width - spaceBetweenCells*3) / 3;
    CGFloat height = (self.collectionView.frame.size.height - spaceBetweenCells*3) / 3; 
    return CGSizeMake(width, height);
}

Hope this helps.

Upvotes: 1

Related Questions