SwiftUI ScrollView content height

1.3k views Asked by At

I have a problem trying to determine the height of a view inside a ScrollView

struct ContentView: View {    
    var body: some View {
        ScrollView{
             GeometryReader { geometry in
                   VStack{
                        ForEach(0..<90) { index in
                             Text("test \(geometry.size.height)")
                        }
                   }
             }
        }
    }
}

geometry.size.height is always 10.000.

how can i correct this unexpected result?

Thanks so much!

1

There are 1 answers

1
vacawama On BEST ANSWER

I'm not sure where you're going with this.

GeometryReader reads the size offered to the view, not the size of the view. You can find out how big the view is by putting a GeometryReader in an .overlay of the VStack, because by the time the overlay is created, the size of the VStack will have been established and that size will be offered to the overlay.

struct ContentView: View {
    var body: some View {
        ScrollView{
           VStack{
                ForEach(0..<90) { index in
                     Text("test 000000000000")
                         .opacity(0)
                }
           }
           .overlay(
                GeometryReader { geometry in
                    VStack {
                        ForEach(0..<90) { index in
                            Text("test \(geometry.size.height)")
                        }
                    }
                }
           )
        }
    }
}