Porting UICollectionView To SwiftUI

Add the power of UIKit to your SwiftUI toolbox.

Jimmy M Andersson
The Startup
Published in
4 min readJun 22, 2020

--

Photo by Emile Perron on Unsplash

SwiftUI has spellbound an entire developer community since the introduction at WWDC 2019, and it is quite easy to understand why. It is fast, concise, and lets you build an entire working application with very few lines of code.

However, SwiftUI is still in its infancy and lacks many of the standard components we take for granted with more mature frameworks. Luckily, Apple’s engineers have developed ways to connect elements from UIKit straight into our SwiftUI applications. In this article, we look at how to leverage those mechanisms to create a SwiftUI wrapper for the good old UICollectionView.

A playground with code examples is available on Github.

What Do We Need?

Before we can connect a UIKit component to SwiftUI, we need to understand the contact surface between the two. The ways that the two frameworks are allowed to communicate with each other will significantly impact the way we write our code. Let’s start by looking at the protocol that enables us to create a wrapper in the first place.

UIViewRepresentable

The UIViewRepresentable protocol gives SwiftUI the possibility to create, manage, and dispose of UIKit components by…

--

--

The Startup
The Startup

Published in The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +772K followers.

Jimmy M Andersson
Jimmy M Andersson

Written by Jimmy M Andersson

Software engineer with a passion for data and machine learning