0

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!

2
  • What were you expecting? Commented Oct 18, 2020 at 20:49
  • 90 line of text >450 ... thanks! Commented Oct 18, 2020 at 21:01

1 Answer 1

2

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)")
                        }
                    }
                }
           )
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@Mofawaw, the OP indicated in the comments above that they expected 90 lines of text >450 which is why the view is repeated in the .overlay.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.