A project for studying of UICollectionViewCompositionalLayout

Overview

UICollectionViewCompositionalLayout

A project for studying of UICollectionViewCompositionalLayout

Let's see the result of project

How will you make this layout with UIKit? Using nesting CollectionViews? It should be hard. But it's getting simpler when you use UICollectionViewCompositionalLayout

UICollectionViewCompositionalLayout


It inherits UICollectionViewLayout. It is used to combine variety layouts. It is composed of 3 type of component

Section (NSCollectionLayoutSection)


It is a container that takes a Group. CollectionView can have one or multiple sections. Section is area of each layout. Section is determined by NSCollectionLayoutGroup. Each section can have a unique background, header, footer.

Group (NSCollectionLayoutGroup)


It is a container that takes items. It acts similar to LayoutItem because it inherited NSCollectionLayoutItem. It plays a role in placing items according to a specific path. It just places items not rendering items. It is injected into Section's init function. It is configured it's size by NSCollectionLayoutDimension. I will explain about this concept at below.

Item (NSCollectionLayoutItem)


It is a most basic component of CollectionView. It is a blueprint about (content's) size, space, arrange. In general, item is a cell, but also can be a Supplementary View(ex: Headers, Footers, Decorations). It is configured it's size by NSCollectionLayoutDimension. It is injected into the Group.

NSCollectionLayoutDimension

It determines the size of the item in CollectionView. There are 3 ways.

absolute

let absoluteSize = NSCollectionLayoutSize(widthDimension: .absolute(44), heightDimension: .absolute(44))

It renders always fixed size item.

estimated

let estimatedSize = NSCollectionLayoutSize(widthDimension: .estimated(200), heightDimension: .estimated(100))

You use estimated when the size can be updated in runtime. System calculates real size based on estimated size.

fractional

let fractionalSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.2), heightDimension: .fractionalHeight(0.2))

I highly recommend fractional. It determines size based on the ratio based on the size of container it belong to. Possible value range is from 0 to 1.

Tip

You can check your result quickly using Preview of SwiftUI even in UIKit.

final class MyController: UICollectionViewController {
//.. My code
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Container().edgesIgnoringSafeArea(.all)
    }
    struct Container: UIViewControllerRepresentable {
        func makeUIViewController(context: Context) -> UIViewController {
            return     UINavigationController(rootViewController: MyController())
        }
        func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
        }
        typealias  UIViewControllerType = UIViewController
    }
}

Section1

let item: NSCollectionLayoutItem = .init(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1)))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .absolute(200)), subitems: [item])
let section = NSCollectionLayoutSection(group: group)
section.orthogonalScrollingBehavior = .groupPaging
return section

Section2

let item: NSCollectionLayoutItem = .init(layoutSize: .init(widthDimension: .fractionalWidth(0.25), heightDimension: .absolute(150)))
item.contentInsets.bottom = 16
item.contentInsets.trailing = 16
let group = NSCollectionLayoutGroup.horizontal(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .estimated(500)), subitems: [item])
let section = NSCollectionLayoutSection(group: group)
section.contentInsets.leading = 16
section.boundarySupplementaryItems = [.init(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .absolute(50)), elementKind: ViewController.categoryHeaderId, alignment: .topLeading)]
return section

Section3

let item: NSCollectionLayoutItem = .init(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .absolute(80)))
item.contentInsets.trailing = 32
let group: NSCollectionLayoutGroup = .horizontal(layoutSize: .init(widthDimension: .fractionalWidth(0.8), heightDimension: .estimated(200)), subitems: [item])
let section = NSCollectionLayoutSection(group: group)
section.orthogonalScrollingBehavior = .groupPaging
section.contentInsets.leading = 16
section.contentInsets.top = 16
return section

To use Group Paging, reduce size of the Group not Item.

Section4

let item = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1)))
item.contentInsets.trailing = 16
item.contentInsets.bottom = 16
let group = NSCollectionLayoutGroup.vertical(layoutSize: .init(widthDimension: .fractionalWidth(0.9), heightDimension: .estimated(300)), subitem: item, count: 5)
let section = NSCollectionLayoutSection(group: group)
section.boundarySupplementaryItems = [.init(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .absolute(50)), elementKind: ViewController.playlistHeaderId, alignment: .topLeading)]
section.contentInsets.leading = 16
section.orthogonalScrollingBehavior = .groupPaging
return section

Playlist section has 5 items in a column. Use NSCollectionLayoutGroup.vertical instead of NSCollectionLayoutGroup.horizontal and insert 5 into the count.

Result

From iOS 13, we can implement very complicated ui layout very easily by using CompositionalLayout. You can also see many variety of layouts, check here.

You might also like...
100-Days-of-SwiftUI - Studying through Paul Hudson's 100 Days of SwiftUI
100-Days-of-SwiftUI - Studying through Paul Hudson's 100 Days of SwiftUI

Hacking with SwiftUI 100 Days of SwiftUI Studying through Paul Hudson's "100 Day

ResearchKit app studying Breast Cancer, developed by Sage Bionetworks.

Share the Journey Share the Journey is one of the first five apps built using ResearchKit. Sage Bionetworks' goal in this study is to understand the c

CompositionalLayoutDSL, library to simplify the creation of UICollectionViewCompositionalLayout. It wraps the UIKit API and makes the code shorter and easier to read.
CompositionalLayoutDSL, library to simplify the creation of UICollectionViewCompositionalLayout. It wraps the UIKit API and makes the code shorter and easier to read.

CompositionalLayoutDSL CompositionalLayoutDSL is a Swift library. It makes easier to create compositional layout for collection view. Requirements Doc

Declaretive UICollectionViewCompositionalLayout interface to implement complex collection view layout.
Declaretive UICollectionViewCompositionalLayout interface to implement complex collection view layout.

CompositionalLayoutViewController Example To run the example project, clone the repo, and run pod install from the Example directory first. Requiremen

DSL for UICollectionViewCompositionalLayout
DSL for UICollectionViewCompositionalLayout

ListKit DSL for UICollectionViewCompositionalLayout! About ListKit is DSL for building UICollectionViewCompositionalLayout. You can make UICollectionV

A simple integrated version of iOS 13 Compositional Layout, modified into a way similar to Functional Programming to generate UICollectionViewCompositionalLayout.
A simple integrated version of iOS 13 Compositional Layout, modified into a way similar to Functional Programming to generate UICollectionViewCompositionalLayout.

WWCompositionalLayout A simple integrated version of iOS 13 Compositional Layout, modified into a way similar to Functional Programming to generate UI

CollectionView - UICollectionView using UICollectionViewCompositionalLayout

CollectionView UICollectionView using UICollectionViewCompositionalLayout create

Swift UIKit E-Commerce (UgurShopping)  No StoryBoard   Firebase, FireStore, FirebaseAuth, KingFisher, SwiftEntryKit, ProgressHud, Alamofire UICollectionViewCompositionalLayout, NotificationCenter
Swift UIKit E-Commerce (UgurShopping) No StoryBoard Firebase, FireStore, FirebaseAuth, KingFisher, SwiftEntryKit, ProgressHud, Alamofire UICollectionViewCompositionalLayout, NotificationCenter

Swift UIKit E-Commerce (UgurShopping) No StoryBoard Firebase, FireStore, FirebaseAuth, KingFisher, SwiftEntryKit, ProgressHud, Alamofire UICollectionViewCompositionalLayout, NotificationCenter

ARKit Base Project. Place virtual objects based on WWDC example project
ARKit Base Project. Place virtual objects based on WWDC example project

ARKit - Placing Virtual Objects in Augmented Reality Learn best practices for visual feedback, gesture interactions, and realistic rendering in AR exp

Don't start from scratch, start from Here! This is a starter project for iOS projects. It contains all the basic configurations and common libraries for your project.

Starter-iOS Don't start from scratch, start from Here! This is a starter project for iOS projects. It contains all the basic configurations and common

100-days-swift-project-8 - The eighth project from 100 days of Swift course
100-days-swift-project-8 - The eighth project from 100 days of Swift course

7 Swifty Words This is the eighth project from Hacking With Swift 100 days of Sw

DrumPadPlayground - Starter Project and Final Project for AudioKit DrumPad Playground App built using Apple's Swift Playgrounds on the iPad NewsAPI-Project - News API Project For iOS
NewsAPI-Project - News API Project For iOS

NewsAPI-Project Es necesario descargar y realizar un pod install para ejecutar e

Save-the-dot-project-swift - Save the dot project with swift
Save-the-dot-project-swift - Save the dot project with swift

Save the Dot Apple introduced UIViewPropertyAnimator for iOS 10. We can use this

ListViewSwiftUI - A project for creating a vertical list using the Swift UI.This project include topic,ListView to show list of movies,Tabbar A starter project for Sample Project in swift 5, Xcode 12.5
A starter project for Sample Project in swift 5, Xcode 12.5

A starter project for Sample Project in swift 5, Xcode 12.5 (also bridging header included so you could use objective c code in it as well ).

A starter project for Sample Project in Objective C.

A starter project for Sample Project in Objective C.

A self-taught project to learn Swift.
A self-taught project to learn Swift.

30 Days of Swift Hi Community I am Allen Wang, a product designer and currently learning Swift. This project was totally inspired by Sam Lu's 100 Days

iOS project template with fastlane lanes, Travis CI jobs and GitHub integrations of Codecov, HoundCI for SwiftLint and Danger
iOS project template with fastlane lanes, Travis CI jobs and GitHub integrations of Codecov, HoundCI for SwiftLint and Danger

iOS project template This repository contains a template for iOS projects with a framework-oriented architecture approach, preconfigured fastlane lane

Releases(v1.0.1)
  • v1.0.1(Feb 11, 2022)

    What's Changed

    • [Release] v1.0.1 by @donggyushin in https://github.com/donggyushin/UICollectionViewCompositionalLayout/pull/7

    Full Changelog: https://github.com/donggyushin/UICollectionViewCompositionalLayout/compare/v1.0.0...v1.0.1

    Source code(tar.gz)
    Source code(zip)
  • v1.0.0(Feb 11, 2022)

    What's Changed

    • [Feature] Add github action by @donggyushin in https://github.com/donggyushin/UICollectionViewCompositionalLayout/pull/1
    • [Release] v1.0.0 by @donggyushin in https://github.com/donggyushin/UICollectionViewCompositionalLayout/pull/4

    Full Changelog: https://github.com/donggyushin/UICollectionViewCompositionalLayout/commits/v1.0.0

    Source code(tar.gz)
    Source code(zip)
Owner
donggyu
What's the business value created by the code I wrote
donggyu
A simple integrated version of iOS 13 Compositional Layout, modified into a way similar to Functional Programming to generate UICollectionViewCompositionalLayout.

WWCompositionalLayout A simple integrated version of iOS 13 Compositional Layout, modified into a way similar to Functional Programming to generate UI

William-Weng 1 Jul 4, 2022
100-days-swift-project-8 - The eighth project from 100 days of Swift course

7 Swifty Words This is the eighth project from Hacking With Swift 100 days of Sw

Bruno Guirra 0 Jan 24, 2022
iOS simple project to create half-screen modal view controller with pan

Simple Half-screen view controller, draggable and less code (learning purpose)

Mohd Hafiz 123 Dec 17, 2022
sample project for `UICollectionViewLayout` implementation

collection-view-layout-pattern-sample UICollectionViewLayout implementation pattern. About This project is introduced in iOSDC Japan 2021. sample code

Toshiki Takezawa 13 Nov 3, 2021
An example project showing how to use `overrideUserInterfaceStyle` to build an in-app light/dark mode switch

OverrideDarkMode A sample project to show how using overrideUserInterfaceStyle enables having a dark / light mode switch directly in the app, while st

Zouhair Mahieddine 1 Jan 19, 2022
Demo project that searches repositories on GitHub and displays details

LookGitUp A test project that queries GitHub and lists the repositories with their name, stars, creation date, user details and user's avatar. Technic

Rashmikant Makwana 0 Nov 30, 2021
The sample project how to use MobileStyleGAN in iOS.

CoreML-StyleGAN The Sample project how to use CoreML model of MobileStyleGAN in the Xcode project. You can generate person images and save it in photo

MLBoy 8 Oct 18, 2022
PhotoCatalog - PhotoCatalog Project Using Swift

PhotoCatalog Project Setup Clone the project from this link [email protected]:islam

Islam Ibrahim 0 Jan 30, 2022
UIKit Practice Project – Simple app to store names along with photos of people you've met

People UIKit Practice Project #10 – Simple app to store names along with photos of people you've met Cool Features Light & dark mode support Responsiv

foxster.mp4 2 Nov 28, 2022
Little project made in swiftUI for an app.

Jou: It's all about jou. A mental wellness app. The project is made in Swift (with the help of SwiftUI) by a team of five members. Main views Mood tra

null 4 Dec 2, 2022