How to Build CollectionView in SwiftUI

CollectionView, HStack, VStack, List, Modifiers in SwiftUI

Amit Rai
4 min readJan 28, 2020

SwiftUI is an innovative, exceptionally simple way to build user interfaces across all Apple platforms with the power of Swift. Build user interfaces for any Apple device using just one set of tools and APIs.

SwiftUI uses a declarative syntax so you can simply state what your user interface should do. Your first line of SwiftUI code is already the most powerful UI code you’ve ever written.

This piece aims to create CollectionView in SwiftUI.

If you are preparing for your technical coding interview or you want to learn recursion to improve your problem-solving skills then you should check this udemy course Recursion Masterclass from Beginner to Advance level in C++ or you can checkout this recursion course on Skillshare.

If you want to learn ARKit 3 from beginner to expert level then click here to get the course and also you will get a 97% discount.

If you are passionate about learning mobile development for iOS and looking to take your iOS development skills to the next level, Core Data with CloudKit framework should be at the top of your list. Click here to get the course and also you will get a 97% discount.

Learn SwiftUI from Scratch click here to get the course because in this course we are going to build many apps using SwiftUI such as Facebook clone, News app, Notes app and much more.

SwiftUI will not provide collection views or table views. Instead, you will use a List component to render lists of content. That said, you can of course wrap a UICollectionViewController in a SwiftUI View. So in this article, we are using List and Stacks to render the CollectionView.

Getting Started

Open up Xcode and create a new Xcode project. Choose Single View App in the iOS template section and click Next.

Enter your product name, and select SwiftUI as the user interface. Click Next, and create it on your desktop.

Here’s what you see once you are done with the setup.

By default, SwiftUI view files declare two structures. The first structure conforms to the View protocol and describes the view’s content and layout. The second structure declares a preview for that view.

Implementation

import SwiftUIstruct DataModel: Identifiable {
let id: String
let name: String
let imageName: String
}
struct ContentView: View {let data: [DataModel] = [
.init(id: "0", name: "SteveJobs", imageName: "SteveJobs"),
.init(id: "1", name: "Satya Nadella", imageName: "Satya Nadella"),
.init(id: "2", name: "Jeff Bezos", imageName: "Jeff Bezos"),
.init(id: "3", name: "Tim Cook", imageName: "Tim Cook")
]
var body: some View {NavigationView {
List {
ForEach(data) { items in
ForEach(0..<2) { item in
CollectionView(data: items)
}
}
}.navigationBarTitle("CollectionView")
}
}
}
struct CollectionView: View {
let data: DataModel
var body: some View {VStack {HStack {ForEach(0..<2) { items in
Spacer()
Image(self.data.imageName)
.resizable()
.frame(width: 150, height: 150)
.foregroundColor(.yellow)
.clipShape(Circle())
.shadow(radius: 10)
Spacer()
}.padding(.bottom, 16)
}
HStack {
Spacer()
Text(self.data.name)
Spacer()
Text(self.data.name)
Spacer()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView().environment(\.colorScheme, .light)
}
}

In the above code, first, we have one DataModel inherited from the Identifiable (Identifiable protocol is used to identify each row uniquely) protocol in that DataModel we have three variables id, name, and imageName. Then in ContentView, we have initialised our DataModel. After that in the body, we have ourNavigationView in that we have one List in our List I have called my CollectionView and pass the items means our DataModel object.

In CollectionView we then created the object from DataModel and then in the body first we have one VStack in the VStack I have embedded the HStack and then finally I have created the custom ImageView with some modifiers. We then created one HStack and in that HStack we have two Text with Spacer() modifiers.

Build and Run!

The final result looks like this

Conclusion

That’s it for this piece. I hope you enjoyed this piece and you have learned how to create CollectionView in SwiftUI.

SwiftUI is here to stay!

If you are preparing for your technical coding interview or you want to learn recursion to improve your problem-solving skills then you should check this udemy course Recursion Masterclass from Beginner to Advance level in C++ or you can checkout this recursion course on Skillshare.

Additional Resources

If you want to learn ARKit 3 from beginner to expert level then click here to get the course and also you will get a 97% discount.

If you are passionate about learning mobile development for iOS and looking to take your iOS development skills to the next level, Core Data with CloudKit framework should be at the top of your list. Click here to get the course and also you will get a 97% discount.

Learn SwiftUI from Scratch click here to get the course because in this course we are going to build many apps using SwiftUI such as Facebook clone, News app, Notes app and much more.

--

--

Amit Rai

Frontend Developer | iOS Developer | Competitive Programmer