Skip to content

How to Fix iOS UICollectionView Layout Issues

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about How to Fix iOS UICollectionView Layout Issues. We cover key concepts, practical examples, and best practices.

The Problem

UICollectionView is not displaying items correctly:

Cells are missing, overlapping, or in the wrong positions.

Quick Fix

Step 1: Check the layout object

WRONG — no layout:

let collectionView = UICollectionView(frame: .zero)  // crashes without layout

RIGHT:

let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: 100, height: 100)
layout.minimumInteritemSpacing = 8
layout.minimumLineSpacing = 8
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)

Step 2: Register the cell

collectionView.register(MyCell.self, forCellWithReuseIdentifier: "MyCell")

Step 3: Implement data source methods

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return items.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! MyCell
    cell.configure(with: items[indexPath.row])
    return cell
}

Step 4: Fix spacing issues

let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
layout.minimumInteritemSpacing = 8
layout.minimumLineSpacing = 12

Step 5: Invalidate layout on rotation

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    coordinator.animate(alongsideTransition: { _ in
        self.collectionView.collectionViewLayout.invalidateLayout()
    })
}

Step 6: Use compositional layout (iOS 13+)

let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.5), heightDimension: .fractionalHeight(1.0))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(200))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
let section = NSCollectionLayoutSection(group: group)
let layout = UICollectionViewCompositionalLayout(section: section)

Prevention

  • Always call register(_:forCellWithReuseIdentifier:) before dequeuing.
  • Use UICollectionViewCompositionalLayout for complex layouts.
  • Call invalidateLayout() when device orientation changes.

Common Mistakes with uicollectionview layout

  1. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  2. Misunderstanding that String is [Char] with poor performance for large text operations
  3. Using foldl instead of foldl' causing stack overflow on large lists

These mistakes appear frequently in real-world IOS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between UICollectionViewFlowLayout and UICollectionViewCompositionalLayout?

FlowLayout arranges items in a linear grid. CompositionalLayout supports complex layouts with nested groups, orthogonal scrolling, and section-specific arrangements.

Why are my collection view cells empty?

The cell was not registered, the reuse identifier does not match, or the data source method is not implemented. Check all three.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro