Grid layouts help you show many items in rows and columns. They make your app look neat and organized.
Showing a photo gallery with many pictures.
Displaying a list of products in a shopping app.
Creating a calendar view with days in a grid.
Arranging buttons or icons evenly on the screen.
Syntax
iOS Swift
LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible())]) {
// Your views here
}
LazyHGrid(rows: [GridItem(.fixed(50)), GridItem(.fixed(50))]) {
// Your views here
}
LazyVGrid arranges items vertically in columns.
LazyHGrid arranges items horizontally in rows.
Examples
This creates a vertical grid with 2 columns. Items fill rows from left to right.
D. Using 'rows' parameter instead of 'columns' in LazyVGrid
Solution
Step 1: Check parameter name for LazyVGrid
LazyVGrid requires 'columns' parameter, not 'rows'. Using 'rows' causes error.
Step 2: Confirm other parts are valid
Text views can be inside LazyVGrid; ForEach is optional for multiple views.
Final Answer:
Using 'rows' parameter instead of 'columns' in LazyVGrid -> Option D
Quick Check:
LazyVGrid needs 'columns' parameter [OK]
Hint: LazyVGrid uses 'columns', not 'rows' [OK]
Common Mistakes:
Confusing rows and columns parameters
Thinking ForEach is mandatory
Misunderstanding GridItem types
5. You want to create a grid with 3 columns where the first column is fixed width 100, the second is flexible, and the third is adaptive with minimum 80 width. Which code correctly defines the columns array for LazyVGrid?
hard
A. [GridItem(.adaptive(minimum: 80)), GridItem(.fixed(100)), GridItem(.flexible())]
B. [GridItem(.flexible()), GridItem(.fixed(100)), GridItem(.adaptive(minimum: 80))]
C. [GridItem(.fixed(100)), GridItem(.flexible()), GridItem(.adaptive(minimum: 80))]
D. [GridItem(.fixed(100)), GridItem(.adaptive(minimum: 80)), GridItem(.flexible())]
Solution
Step 1: Match column order with requirements
The first column must be fixed width 100, second flexible, third adaptive minimum 80.