NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

How to lazy load views using LazyVStack and LazyHStack

Paul Hudson    @twostraws   

Updated for Xcode 12.0

New in iOS 14

By default, SwiftUI’s VStack and HStack load all their contents up front, which is likely to be slow if you use them inside a scroll view. If you want to load content lazily – i.e., only when it scrolls into view, you should use LazyVStack and LazyHStack as appropriate.

For example, this will display 1000 text views lined up vertically, making sure each one is created only as it’s scrolled into view:

struct ContentView: View {
    var body: some View {
        ScrollView {
            LazyVStack {
                ForEach(1...1000, id: \.self) { value in
                    Text("Row \(value)")
                }
            }
        }
    }
}

Warning: These lazy stacks automatically have a flexible preferred width, so they will take up free space in a way that regular stacks do not. To see the difference, try the code above and you’ll find you can drag around using the whitespace around the text, but if you switch to a regular VStack you’ll see you need to scroll using the text itself.

When using lazy stacks, SwiftUI will automatically create a view when it gets shown for the first time. After that the view will remain memory, so be careful how much you show.

If you want to see how the lazy loading works in practice, try this example:

struct SampleRow: View {
    let id: Int

    var body: some View {
        Text("Row \(id)")
    }

    init(id: Int) {
        print("Loading row \(id)")
        self.id = id
    }
}

struct ContentView: View {
    var body: some View {
        ScrollView {
            LazyVStack {
                ForEach(1...1000, id: \.self, content: SampleRow.init)
            }
        }
    }
}
Hacking with Swift is sponsored by Instabug

SPONSORED Are you tired of wasting time debugging your Swift app? Instabug’s SDK is here to help you minimize debugging time by providing you with complete device details, network logs, and reproduction steps with every bug report. All data is attached automatically, and it only takes a line of code to setup. Start your free trial now and get 3 months off exclusively for the Hacking with Swift Community.

Start your free trial!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 3.8/5

Link copied to your pasteboard.