GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

Project 7 - fast track.

Forums > macOS

Hi working on this book but confused at this point

struct ContentView: View { let gridItems: [GridItem] = [ GridItem(.fixed(100)), GridItem(.fixed(100)), GridItem(.fixed(100)) ]

var body: some View {
    LazyVGrid (columns: gridItems) {
        ForEach(1..<100){ i in Color.red}

    }

}

}

“So, that’s a vertical grid using our three-column structure, and inside that are 99 rows containing the color red – nothing more. When you run that project you’ll notice a few things:

1 The grid runs edge to edge in our window.”

Sadly what I notice is not that. Instead what I get is a resizable window Second adding Scroll View does nothing, Third adding lots and lots and lots of columns doesnt give you scrolling either. Here is the dotty code I used to test that struct ContentView: View { let gridItems: [GridItem] = Array(repeating: GridItem(.fixed(100)), count:15)

]

var body: some View {
    ScrollView{
        LazyVGrid (columns: gridItems) {
            ForEach(1..<1000){ i in Color.red}

        }
    }

}

}

2      

Resizable Window:

Default Behavior: SwiftUI windows are resizable by default. To make the grid fill the entire window and prevent resizing: Swift // Add this modifier to ContentView .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity) Use code with caution. Learn more

  1. ScrollView Not Working:

Missing Frame Modifier: ScrollView needs a defined size to work correctly. Apply this modifier to the LazyVGrid: Swift ScrollView { LazyVGrid(columns: gridItems) { ... } .frame(maxWidth: .infinity) // Allow horizontal scrolling }

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure and A/B test your entire paywall UI without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.