我需要根据其中的内容高度更新 ScrollView 的高度。我在下面有一个工作轮播:
struct PostsPaging<Content>: View where Content: View {
@Binding var index: Int
let maxIndex: Int
let content: () -> Content
@State private var offset = CGFloat.zero
init(index: Binding<Int>, maxIndex: Int, @ViewBuilder content: @escaping () -> Content) {
self._index = index
self.maxIndex = maxIndex
self.content = content
}
var body: some View {
ZStack(alignment: .bottomTrailing) {
GeometryReader { geometry in
ScrollView (.horizontal, showsIndicators: false){
LazyHStack(spacing: 0) {
self.content()
.fixedSize()
.frame(width: geometry.size.width)
.clipped()
}
}
.content.offset(x: self.offset(in: geometry), y: 0)
.frame(width: geometry.size.width, alignment: .leading)
.animation(.spring())
}.clipped()
}
}
}
这就是它的名称:
PostsPaging(index: self.$index.animation(), maxIndex: self.posts.count - 1) {
ForEach(self.posts, id: \.self) { post in
SmallPostCard(...)
}
}
这似乎只返回相同的值,一遍又一遍:
.background(
GeometryReader { proxy in
Color.clear.onAppear { print(proxy.size.height) }
})
找出内容高度并更新滚动时轮播高度的最佳方法是什么?