Using LazyHStack and LazyVStack
SwiftUI 2.0 introduced the LazyHStack
and LazyVStack
views. They can be used similar to the regular HStack
and VStack
views (discussed in Chapter 1, Using the Basic SwiftUI Views and Controls) but offer an extra advantage of lazy loading its content. The list's content is loaded just before it becomes visible on the device's screen, allowing the user to seamlessly scroll through large datasets with no perceptible UI lag or long load times. Let's create an app to see how this works in practice.
Getting ready
Create a new SwiftUI app called LazyStacks
.
How to do it…
We will create an app that applies both the LazyHStack
and LazyVStack
views. We will ensure that each list row prints out some text to the console before being loaded. That way, we'll know what is being loaded and when. The steps are as follows:
- Create a
ListRow
view that has two properties: anid
and atype
. TheListRow
view will print out some...