Search code examples
swiftcollectionsviewcollectionviewuicollectionviewcompositionallayout

How to create collection view like image with UICollectionViewCompositionalLayout


For a couple of days I can't figure out how to make a collection view like in the screenshot using UICollectionViewCompositionalLayout. Can anyone help ? Image!!!!


Solution

  • You can achieve this layout combining two vertical NSCollectionLayoutGroup in a horizontal group. Each vertical group is composed by two itens, a big and other small.

    final class CustomLayout {
    
    
    
    class func create() -> UICollectionViewCompositionalLayout{
        
        //Obs: The size of each element is relative to his parent.
        //NSCollectionLayoutItem < NSCollectionLayoutGroup < NSCollectionLayoutSection < UICollectionViewCompositionalLayout
        
        
        
        //Defines the margin
        let margin: CGFloat = 5
        //Defines the space between the cells
        let contentInsests: CGFloat = 8
    
        
        // Create the big item that is 60% of your parent's height
        let bigItem = NSCollectionLayoutItem(
            layoutSize: NSCollectionLayoutSize(
                widthDimension: .fractionalWidth(1.0),
                heightDimension: .fractionalHeight(0.60)))
        
        bigItem.contentInsets = NSDirectionalEdgeInsets(
            top: contentInsests,
            leading: contentInsests,
            bottom: contentInsests,
            trailing: contentInsests)
        
        // Create the small item that is 40% of your parent's height
        let smallItem = NSCollectionLayoutItem(
            layoutSize: NSCollectionLayoutSize(
                widthDimension: .fractionalWidth(1.0),
                heightDimension: .fractionalHeight(0.40)))
        
        smallItem.contentInsets = NSDirectionalEdgeInsets(
            top: contentInsests,
            leading: contentInsests,
            bottom: contentInsests,
            trailing: contentInsests)
        
    
        //Here we Combine the two itens in a vertical group
        let group1 = NSCollectionLayoutGroup.vertical(
            layoutSize: NSCollectionLayoutSize(
                widthDimension: .fractionalWidth(0.5),
                heightDimension: .fractionalHeight(1.0)),
            subitems: [bigItem,smallItem])
        
        //Here we do the same reversing the order of the itens
        let group1Reversed = NSCollectionLayoutGroup.vertical(
            layoutSize: NSCollectionLayoutSize(
                widthDimension: .fractionalWidth(0.5),
                heightDimension: .fractionalHeight(1.0)),
            subitems: [smallItem, bigItem])
        
        
        //here we combine both in a nested group making the layout like in the image.
        //In this example we are making a custom layout with two "Image layout" per screen, so
       // We define his height as 0.5% of his parent's height. You can change it for your preference.
        let nestedGroup = NSCollectionLayoutGroup.horizontal(
            layoutSize: NSCollectionLayoutSize(
                widthDimension: .fractionalWidth(1.0),
                heightDimension: .fractionalHeight(0.5)),
            subitems: [
              group1,
            group1Reversed
            ]
        )
        
        
        //Here is our main group with two nested group
        let mainGroup = NSCollectionLayoutGroup.vertical(
            layoutSize: NSCollectionLayoutSize(
                widthDimension: .fractionalWidth(1.0),
                heightDimension: .fractionalHeight(1.0)),
            subitems: [
              nestedGroup,nestedGroup
            ]
        )
        
        
        
        let section = NSCollectionLayoutSection(group: mainGroup)
        
        
        section.contentInsets = NSDirectionalEdgeInsets(
            top: 0,
            leading: margin,
            bottom: 0,
            trailing: margin)
        
        let configuration = UICollectionViewCompositionalLayoutConfiguration()
        configuration.scrollDirection = .vertical
        let layout = UICollectionViewCompositionalLayout(section: section)
        layout.configuration = configuration
        return layout
    }}
    

    Example