SwiftUI LazyHStack vs HStack

If we want to load the content into memory when we need it, we can use LazyHStack. It is particularly useful when we have large list of contents inside a scrollview.

Here’s how our regular HStack looks like:

HStack has 1000 Text content in it and the background color of HStack is blue:

And this is the memory report I have at first launch of my simulator:

Now back to LazyHStack:

Exactly same setup, I only replaced HStack with LazyHStack and here’s the result:

See that LazyHStack has flexible height and it covers all the screen whereas HStack fits in its context. With LazyHStack, in this example, we can scroll horizontally by dragging anywhere on the screen.

And the memory report at first launch of my simulator:

--

--