SWIFTUI: LazyVGrid y LazyHGrid (Crea Grids para tus vistas en SwiftUI) en Español #18

By SwiftBeta

Share:

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

  1. Project Setup: The video starts by creating a new SwiftUI project.
  2. Initial Grid Creation: Inside the body of the ContentView, a LazyVGrid is added. The initializer used takes a GridItem array as an argument, defining the structure of the grid.
  3. Placeholder Content: Initially, the grid contains placeholder text. The presenter notes that the text might not display correctly due to the default GridItem configuration.
  4. Fixed Grid Items: The initial GridItem is of type fixed with 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.
  5. Multiple Columns: The video demonstrates how to create multiple columns by adding more GridItem instances to the array. Each GridItem represents a column in the grid.

Practical Example: Displaying 500 Views in a Grid

  1. Data Generation: The video creates a property called elements which is an array containing a range of 500 numbers. This will serve as the data source for the grid.
  2. Grid Item Property: A gridItems property is created to hold the GridItem configurations. This property is then passed to the LazyVGrid.
  3. ForEach Loop: Inside the LazyVGrid, a ForEach loop iterates through the elements array. For each element, a Text view is created, displaying the element's value.
  4. ScrollView Integration: The presenter highlights that the grid is not scrollable by default. To enable scrolling, the LazyVGrid must be embedded within a ScrollView. The presenter emphasizes the importance of this step.
  5. 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

  1. Fixed Size: The default GridItem type is fixed, where the width is explicitly set.
  2. Adaptive Size: The video introduces the adaptive GridItem type. With adaptive, 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.
  3. Flexible Size: The video explains the flexible GridItem type. 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

  1. Switching to Horizontal Grid: The video demonstrates how to switch from LazyVGrid to LazyHGrid. The presenter notes that the columns parameter needs to be changed to rows to reflect the horizontal orientation.
  2. ScrollView Configuration: When using LazyHGrid within a ScrollView, the ScrollView's axis parameter must be set to .horizontal to enable horizontal scrolling.
  3. 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

  • LazyVGrid and LazyHGrid are used for creating grid layouts in SwiftUI.
  • LazyVGrid arranges items vertically, while LazyHGrid arranges them horizontally.
  • GridItem defines the properties of each column or row in the grid.
  • GridItem can be fixed, flexible, or adaptive, controlling the size and behavior of the views within the grid.
  • ScrollView is required to enable scrolling in LazyVGrid and LazyHGrid.
  • Lazy loading optimizes performance by loading views on demand.

Chat with this Video

AI-Powered

Load the transcript when you're ready to chat so the initial page stays lighter.

Ready to summarize another video?

Summarize YouTube Video