UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.