SwiftUI:获取 ScrollView 内内容的高度以更新框架

问题描述

我需要根据其中的内容高度更新我的 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) }
})

找出内容高度并在滚动时更新轮播高度的最佳方法是什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)