SWIFTUI: LazyVGrid y LazyHGrid (Crea Grids para tus vistas en SwiftUI) en Español #18
By SwiftBeta
Key Concepts
LazyVGrid, LazyHGrid, GridItem (fixed, flexible, adaptive), ScrollView, column customization, row customization, UI customization, performance optimization (lazy loading).
LazyVGrid and LazyHGrid: Introduction to Grid Layouts in SwiftUI
The video introduces LazyVGrid and LazyHGrid in SwiftUI, which are views that arrange other views in a grid format. The primary difference lies in their orientation: LazyVGrid arranges views vertically, while LazyHGrid arranges them horizontally. The video emphasizes the customization options available for the views within these grids, particularly concerning their sizes (fixed, flexible, or adaptive).
Basic Implementation of LazyVGrid
- Project Setup: The video starts by creating a new SwiftUI project.
- Initial Grid Creation: Inside the
bodyof theContentView, aLazyVGridis added. The initializer used takes aGridItemarray as an argument, defining the structure of the grid. - Placeholder Content: Initially, the grid contains placeholder text. The presenter notes that the text might not display correctly due to the default
GridItemconfiguration. - Fixed Grid Items: The initial
GridItemis of typefixedwith a width of 20. This means each view inside the grid has a fixed width of 20 points. The presenter modifies this to 100 to better display the text. - Multiple Columns: The video demonstrates how to create multiple columns by adding more
GridIteminstances to the array. EachGridItemrepresents a column in the grid.
Practical Example: Displaying 500 Views in a Grid
- Data Generation: The video creates a property called
elementswhich is an array containing a range of 500 numbers. This will serve as the data source for the grid. - Grid Item Property: A
gridItemsproperty is created to hold theGridItemconfigurations. This property is then passed to theLazyVGrid. - ForEach Loop: Inside the
LazyVGrid, aForEachloop iterates through theelementsarray. For each element, aTextview is created, displaying the element's value. - ScrollView Integration: The presenter highlights that the grid is not scrollable by default. To enable scrolling, the
LazyVGridmust be embedded within aScrollView. The presenter emphasizes the importance of this step. - Lazy Loading Explanation: The video explains the "lazy" aspect of
LazyVGrid. It means that the views are not loaded into memory all at once. Instead, they are loaded on demand as the user scrolls through the grid. This is a performance optimization technique, especially useful when dealing with a large number of views (e.g., 5000). Only the views that are currently visible or about to become visible are loaded.
Customizing Grid Items: Fixed, Flexible, and Adaptive
- Fixed Size: The default
GridItemtype isfixed, where the width is explicitly set. - Adaptive Size: The video introduces the
adaptiveGridItemtype. Withadaptive, you specify a minimum width. The grid then tries to fit as many views as possible within each row, respecting the minimum width constraint. The number of columns is not fixed and can vary depending on the available space. - Flexible Size: The video explains the
flexibleGridItemtype. This allows a column to take up available space. Multiple flexible columns can be created, sharing the available space. The presenter demonstrates how to create a grid with fixed-size columns on the sides and flexible columns in the middle.
LazyHGrid Demonstration
- Switching to Horizontal Grid: The video demonstrates how to switch from
LazyVGridtoLazyHGrid. The presenter notes that thecolumnsparameter needs to be changed torowsto reflect the horizontal orientation. - ScrollView Configuration: When using
LazyHGridwithin aScrollView, theScrollView'saxisparameter must be set to.horizontalto enable horizontal scrolling. - Horizontal Scrolling: The presenter shows how the grid now scrolls horizontally, displaying the views in a horizontal arrangement.
Conclusion
The video concludes by summarizing the key points: LazyVGrid and LazyHGrid are powerful tools for creating grid layouts in SwiftUI. They offer flexibility in terms of orientation (vertical or horizontal) and customization of the views within the grid (fixed, flexible, or adaptive sizes). The "lazy" loading behavior optimizes performance by loading views on demand. The presenter encourages viewers to experiment with these views to create custom UIs.
Key Takeaways
LazyVGridandLazyHGridare used for creating grid layouts in SwiftUI.LazyVGridarranges items vertically, whileLazyHGridarranges them horizontally.GridItemdefines the properties of each column or row in the grid.GridItemcan befixed,flexible, oradaptive, controlling the size and behavior of the views within the grid.ScrollViewis required to enable scrolling inLazyVGridandLazyHGrid.- Lazy loading optimizes performance by loading views on demand.
Chat with this Video
AI-PoweredLoad the transcript when you're ready to chat so the initial page stays lighter.
Related Videos

Shocking video shows moment paramedics are hit by Israel in 'double-tap' strike
Sky News

Inside Jeffrey Epstein's Network of Power
Bloomberg Originals

Trump's supporters furious over Trump smartphone scam.
ABC News In-depth

Throwing out the first pitch for the Rockies for STEM Day!
Sick Science!

Samsung union suspends strike after reaching tentative pay deal • FRANCE 24 English
FRANCE 24 English

OH SH*T! The Banks are Dumping AI Loans!
Steven Van Metre

SixDegrees.com Founder Andrew Weinreich On Social Media Addiction
Forbes