UICollectionViewCompositionalLayout

UIKit Mid-level Swift 5.0+ iOS 17+ Reviewed By Alok Singh

Pick the fix

You need horizontally scrolling rows of cards inside a vertical list, like the App Store. Best approach?

Options
Hint 1 of 1

Which property makes a section scroll sideways?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Compositional layout sections with orthogonalScrollingBehavior = .groupPaging

Correct. One collection view, and each section scrolls sideways.

Why not the others?
  • B A collection view inside each table view cell. It's the old workaround: more code, nested reuse and scroll bugs.
  • C A UICollectionViewFlowLayout subclass. Flow layout scrolls in one direction only.
  • D A UIScrollView per row. There is no reuse, so it doesn't scale.

30-second answer

A compositional layout builds sections from items, groups and sections, each sized absolutely, fractionally or by estimate. Each section can have its own layout, and orthogonalScrollingBehavior makes a section scroll sideways inside a vertical list. It replaces nested collection views and most custom layout subclasses.

Practise saying it · 30 s. No recording, just a timer

Explain compositional layouts.

Say your answer out loud, then check which of these points you covered:

  • Items, groups, sections
  • Per-section layouts
  • Orthogonal scrolling

Deep dive

Use .estimated heights for self-sizing cells.

Supplementary views add headers and badges; decoration items add backgrounds.

UICollectionViewCompositionalLayout { sectionIndex, environment in } builds sections dynamically.

List configurations are compositional layouts too.

Common mistakes

  • Nesting collection views in table cells.
  • Subclassing flow layout for multi-section designs.

Senior take & follow-ups

Seniors explain the item, group and section hierarchy and orthogonal scrolling.

Follow-up questions

Think of your answer first, then open the hint.

  1. How do you size a cell to its content?

    Show hint

    Estimated dimension plus Auto Layout in the cell.

  2. How do you give each section a different layout?

    Show hint

    Return a different NSCollectionLayoutSection per index.

From the author

Catalog - UI

Made by the author: browse UIKit components with live demos, and copy their code snippets into your project.

Suggest a fix if something here is wrong or out of date.