Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create grid in SwiftUI

Tags:

swift

swiftui

I know that we can create a List in vertical SwiftUI like this,

struct ContentView : View {
    var body: some View {
        NavigationView {
            List {
                Text("Hello")
            }
        }
    }
}

but is there any way that we could split the list in 2 or 3 or maybe more spans that covers the screen like a grid like we did in UICollectionView

like image 813
SinaMN75 Avatar asked Oct 30 '25 03:10

SinaMN75


2 Answers

Checkout ZStack based example here

Grid(0...100) { _ in
    Rectangle()
        .foregroundColor(.blue)
}

enter image description here

like image 146
Alex Avatar answered Oct 31 '25 18:10

Alex


iOS 14

There is 2 new native Views that you can use:

  1. LazyHGrid HGrid

  2. LazyVGrid VGrid

With code or directly from the library:

Preview

The library contains a fully working sample code that you can test yourself.

like image 27
Mojtaba Hosseini Avatar answered Oct 31 '25 16:10

Mojtaba Hosseini