getting size of scroll view in swift

Hello, I have been playing around with preference keys and geometry readers but I can't figure out a way to actually get the real size of a scrollview or vstack inside a scroll view. I have 2 different approaches and they both semi work, the problem with them is that when I scroll through the scroll view the size fluctuates. For example if the size of the vstack in the scroll view is 5000, then the size I get from my methods will fluctuate between 4200-5900 as I scroll through the view. Im not sure why this is, can anyone show me a solid way to get the height of a vstack inside a scrollview.

method 1

                    ScrollView {
                        LazyVStack {
                            ForEach(viewModel.new.) { i in
                                   someView(i)
                            }
                        }
                        .background(GeometryReader { proxy in
                            Color.clear
                                .onChange(of: offset) { _ in
                                    print("Height of VStack:", proxy.size.height)
                                }
                        })
                    }

method 2

                    ScrollView {
                        ChildSizeReader(size: $scrollViewSize) {
                            LazyVStack { 
                                ForEach(viewModel.new) { i in
                                        someView(i)
                                    }
                                }
                            }
                        }
                    }
struct ChildSizeReader<Content: View>: View {
  @Binding var size: CGSize
  let content: () -> Content
  var body: some View {
    ZStack {
      content().background(
        GeometryReader { proxy in
          Color.clear.preference(
            key: SizePreferenceKey.self,
            value: proxy.size
          }
        }
      }
    }
    .onPreferenceChange(SizePreferenceKey.self) { preferences in
      self.size = preferences
    }
  }
}

struct SizePreferenceKey: PreferenceKey {
  typealias Value = CGSize
  static var defaultValue: Value = .zero

  static func reduce(value _: inout Value, nextValue: () -> Value) {
    _ = nextValue()
  }
}

Is it the real code ? This should not compile:

                            ForEach(viewModel.new.) { i in

Same for

                .background(GeometryReader { proxy in
                    Color.clear

Did you get model on this ? https://developer.apple.com/forums/thread/650312

getting size of scroll view in swift
 
 
Q