Understanding Grids in SwiftUI for iOS 14

Understanding Grids in SwiftUI for iOS 14

SwiftUI was announced at WWDC 2019 and it paved a way for declarative programming for building interfaces. SwiftUI Grid comes in two different flavours, which includes LazyVGrid and LazyHGrid. As the name suggest, LazyVGrid is a Grid that flows in vertical direction and the LazyHGrid flows in horizontal direction.

SwiftUI was announced at WWDC 2019 and it paved a way for declarative programming for building interfaces. SwiftUI Grid comes in two different flavours, which includes LazyVGrid and LazyHGrid. As the name suggest, LazyVGrid is a Grid that flows in vertical direction and the LazyHGrid flows in horizontal direction.

If you are interested in videos then check out the following videos associated with this post.

Each Grid consists of GridItem, which dictates the structure of the cell displayed in the grid. GridItem can be fixedflexible or adaptive. Let’s start with the fixed size GridItems.

Fixed GridItems

var columns: [GridItem] = [

            GridItem(.fixed(100)),
            GridItem(.fixed(100)),
            GridItem(.fixed(100))

        ]

In the above code, we have created 3 fixed size columns of size 100. Now, we an easily use the LazyVGrid to create the columns and display information in the GridItem. This is shown below:

 NavigationView {

                LazyVGrid(columns: columns) {

                    Rectangle()
                        .fill(Color.red)
                        .aspectRatio(contentMode: .fit)

                    Rectangle()
                        .fill(Color.red)

                    Rectangle()
                        .fill(Color.red)

                }

                .navigationTitle("LazyVGrid")
            }

As you can see that for the content of LazyVGrid, we have used Rectangle but you can use any View you desire.

You can add more Rectangles to the LazyVGrid by running a ForEach. A ForEach loop will return a type View with each iteration. The implementation is shown below:

 NavigationView {

                ScrollView {
                LazyVGrid(columns: columns) {

                    ForEach(1..<20) { _ in
                        Rectangle()
                            .fill(Color.red)
                            .aspectRatio(contentMode: .fit)
                    }

                }}

                .navigationTitle("LazyVGrid")
            }

ios-14 ios-app-development swift-programming swiftui programming

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.

Top Swift iOS App Development Company in USA

AppClues Infotech is a top Mobile App Development Company in USA building high-quality Android, iOS, and Native apps for Startups, SMBs, & Enterprises. Contact us now!

Best Swift App Development Company

Apple Swift App Development India: Mobile App Development India is an expert iOS Swift App Development company in India. We develop secure and robust mobile app solutions based on new Apple Swift language.

Hire iOS App Developer

Are you looking to transform your idea into an iPhone application? Hire iPhone programmer team from **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")** to ensure the best results, utilizing all the latest trends in iOS app...

Top iOS App Development Company in USA

AppClues Infotech is a top iOS mobile app development company in USA offering custom iPhone app development services for startups & enterprises of all sizes.