如果不在 ScrollView 或 List 内,LazyVStack 就不懒惰吗?

问题描述 投票:0回答:1

看来 LazyVStack 只有在 ScrollView 或 List 中时才“懒惰”?

代码如下:

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
    }
}

我目前正在尝试创建一个纯 SwiftUI ScrollView,但我陷入了困境。

我尝试使用

.clipped()
并将
.frame
设置为
ContainerView
LazyVStack
。有什么建议吗?

swiftui
1个回答
0
投票

看来 LazyVStack 只有在 ScrollView 或 List 中时才“懒惰”?

是的,这是真的

© www.soinside.com 2019 - 2024. All rights reserved.