UICollectionView in 2 columns

1

Friends, I have a IUCollectionView that is presenting the data in 3 columns, how could I do to only show 2 with a centralized image?

    
asked by anonymous 16.02.2017 / 21:46

1 answer

0

In order for O UICollectionView to present the cells as expected, you have two options:

  • Increase the size of your cell; or
  • Increase the spaces between your cells and the borders.
  • 1) Increase the size of your cell

    In this case, you can simply change the size of the cell in UIStoryboard (easier / faster); or explicitly declare the CGSize that you expect the cell to implement the collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize ( UICollectionViewDelegateFlowLayout ) method

    2) Increase the spaces between your cells and the borders

    In this case, you can implement the UICollectionViewDelegateFlowLayout protocol methods for this, such as:

    • collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets
    • collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat
    • collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat

    Hands-on

    Suppose you want the cell to have a width of 100px per 100px height with padding of 20px between the edges and the middle. The implementation will be (more or less) like this (not tested):

    Swift 2

    let cellDimension : CGFloat = 100
    let spaceBetweenCells : CGFloat = 20
    let numberOfColumns : CGFloat = 2
    
    ....
    
    // Layout
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSize(width: cellDimension, height: cellDimension)
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        let width = self.view.frame.width
        let expectUsedSize : CGFloat = (cellDimension * numberOfColumns) + spaceBetweenCells
        let margins = (width - expectUsedSize) / numberOfColumns
        return UIEdgeInsetsMake(spaceBetweenCells, margins, spaceBetweenCells, margins)
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
        return spaceBetweenCells
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
        return spaceBetweenCells
    }
    

    Swift 3

    // Layout
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: cellDimension, height: cellDimension)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        let width = self.view.frame.width
        let expectUsedSize : CGFloat = (cellDimension * numberOfColumns) + spaceBetweenCells
        let margins = (width - expectUsedSize) / numberOfColumns
        return UIEdgeInsetsMake(spaceBetweenCells, margins, spaceBetweenCells, margins)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return spaceBetweenCells
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return spaceBetweenCells
    }
    

    Note

    In the above code, I used the same space between cells ( spaceBetweenCells ) for UIEdgeInsets of cell (bottom and top). You can (obviously) change this value however you want.

    Abs!

    The above implementation is intended to illustrate the algorithm that calculates the right cell size for different device widths (iPhone 4s / 5 / 5c / 5s / 6 / 6s / 7).

    / 7s or iPads);

        
    06.03.2017 / 20:31