By the end of this lesson, students should be able to:
- Identify the different elements of a Compositional Layout
- Implement Compositional Layouts using different sections
When we need to create more complex layouts, we find ourselves in a situation where we need to play with combinations of table views and collection views.
This is doable but definitely increases the level of complexity. 🤯
In June 2019, Apple gave us an update for collection views: an API called Compositional Layout. 🧩
With this new API is easier than ever to create any layout you might need.
- Download this starter project
- Run it, you should see something like this:
- Go to
BasicGridSection.swift
and find theitemSize
constant. - Change
.fractionWidth(0.2)
to.fractionalWidth(0.5)
- Run it again. What do you get now?
- Go back to where you changed the
itemSize
and now update.fractionWidth(0.5)
to.fractionalWidth(1.0)
- Now find the
groupSize
constant and change.fractionalHeight(0.1)
to.fractionalHeight(0.25)
- Run it again. What do you get now?
- Find the
group
andsection
constants. - Update the axis to be vertical.
- After the section declaration add
section.orthogonalScrollingBehavior = .continuous
- Run it again. What do you get now?
From this point, see if you can get 4 rows of items scrolling horizontally.
Needs 3 elements:
NSCollectionLayoutItem
- a single element inside a groupNSCollectionLayoutGroup
- a single group within a sectionNSCollectionLayoutSection
- all groups combined
A grid that scrolls horizontally
A group with 3 items. Axis: vertical.
Used to determine the size of an Item or a Group.
- Fractional width: a fraction of its parent’s full width.
- Fractional height: a fraction of its parent’s full height.
- Absolute: a specific value for the size.
- Estimate: a specific a value but the value can grow or shrink.
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1))
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1))
let groupSize = NSCollectionLayoutSize(widthDimension: .absolute(200), heightDimension: .absolute(300))
A view controller can have an array of sections. These sections are then passed to the collection view layout.
This protocol can help all sections follow the same structure.
protocol Section {
var numberOfItems: Int { get }
func layoutSection() -> NSCollectionLayoutSection
func configureCell(collectionView: UICollectionView, indexPath: IndexPath) -> UICollectionViewCell
}
layoutSection() - where we create and pass back everything needed for compositional layout configureCell() - called to create the cell for the section
Work on TitleSection.swift
- Create the number of items.
- Create the constant to hold the title
- Add an initializer
Step 1: Create an item size. Make it full width and height.
Step 2: Create an item and give it the item size we already have.
Step 3: Create the size of the group. Set it to the full width and make the height 70.
Step 4: Pass the group size and item to the group.
Step 5: Pass the group to the section.
Step 6 Create the cell and pass it to the collectionView
We need to add the new section to the array in our View Controller.
lazy var sections: [Section] = [
TitleSection(title: "Grid Example"),
BasicGridSection(),
TitleSection(title: "Featured Categories"),
TitleSection(title: "Last Month's Favorites")
]
Examine UICollectionViewDataSource
to see how we are populating the collectionView.
Run the app after.
Number of sections is given by the count of our sections array. The number of items per section is the property we created in each section. The sections array along with indexPath.section is used to access the correct configureCell() method for that section.Complete the sections for FavoriteSection
and FeaturedSection
.
Create a View Controller for the subscription box app to display several sections.
- Featured categories
- Last month's favorite items
This view controller will become our home screen :)
You can see how it should look like in the online design.
Today's activity was based on a chapter in Swift For Good Vol.1 by Craig Clayton (@thedevme)
WWDC - Advances in CollectionView Layout
Tutorial - Compositional Layouts & Diffable data sources
Ray Wenderlich tutorial
App Store Layout - Article