看来 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
。有什么建议吗?
看来 LazyVStack 只有在 ScrollView 或 List 中时才“懒惰”?
是的,这是真的