LazyVStack not lazy if not within a ScrollView or List?

Viewed 1360

It seems that LazyVStack is only "lazy" when within a ScrollView or List?

Code below:

struct ContentView: View {
    
    var body: some View {
        makeBody()
    }
    
    private func makeBody() -> some View {
        // ScrollView { // uncomment to see difference
        ContainerView { //
            LazyVStack {
                ForEach(1...100, id: \.self) {
                    WrappedText(s: String($0))
                }
            }
        }
        
    }
    
    struct WrappedText: View {
        var s: String

        var body: some View {
            makeBody()
        }
        
        private func makeBody() -> some View {
            print("Wrapped text body: \(s)") // this is called for all texts during initialisation (if not in a ScrollView / List)
            return Text(String(s))
        }
    }
}

struct ContainerView<Content: View>: View {

    let content: Content

    init(@ViewBuilder content: () -> Content) {
        self.content = content()
    }

    var body: some View {
        return self.content
    }
}

I'm currently experimenting with creating a pure SwiftUI ScrollView, and am stuck on this.

I've tried using .clipped() and setting the .frame on ContainerView and LazyVStack. Any suggestions?

0 Answers
Related