2

I am able to print scrollview content height based on the below code when the countvalue is static

struct ContentView: View {

@State var countValue : Int = 1000
    var body: some View {
        ScrollView {
            ForEach(0..<countValue) { i in
                Text("\(i)")
            }
            .background(
                GeometryReader { proxy in
                    Color.clear.onAppear { print(proxy.size.height) }
                }
            )
        }
    }
}

But when i Updated countValue in runtime, I not able to print the new scrollview contentsize height

Please Refer the Below Code

struct ContentCountView: View {

@State var countValue : Int = 100
    var body: some View {
        ScrollView {
            ForEach(0..<countValue, id: \.self) { i in
                HStack{
                    Text("\(i)")
                    Button("update"){
                        countValue = 150
                    }
                }
                
            }
            .background(
                GeometryReader { proxy in
                    Color.clear.onAppear {
                        print(proxy.size.height)
                        
                    }
                }
            )
        }
    }
}

how can I get the new scrollview content size height? Please explain.

Maharajan S
  • 93
  • 10

2 Answers2

1

proxy.size.height is updating, putting the print statement in onAppear just limits the printing to when it first appears. Try this:

.background(
    GeometryReader { proxy in
         let _ = print(proxy.size.height)
         Color.clear
    }
)
Steve M
  • 9,296
  • 11
  • 49
  • 98
1
        .background(
            GeometryReader { proxy in
                Color.clear
                    .onChange(of: proxy.size) { newValue in
                        contentHeight = newValue.height
                    }
            }
        )
  • 1
    Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. **Would you kindly [edit] your answer to include additional details for the benefit of the community?** – Jeremy Caney Jul 21 '23 at 00:53