I need to update the height of my ScrollView depending on the content height inside of it. I have a working carousel below:
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()
}
}
}
This is how it's called:
PostsPaging(index: self.$index.animation(), maxIndex: self.posts.count - 1) {
ForEach(self.posts, id: \.self) { post in
SmallPostCard(...)
}
}
This seems to only return the same value, over and over again:
.background(
GeometryReader { proxy in
Color.clear.onAppear { print(proxy.size.height) }
})
What would be the best way to find out the height of the content and update the height of the carousel on scroll?