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.

2
  • This should be helpful - stackoverflow.com/a/65062892/12299030. Commented Nov 12, 2021 at 10:28
  • this not explain about scrollview content size height, can you please explain how to get content size height Commented Nov 12, 2021 at 10:40

2 Answers 2

1
        .background(
            GeometryReader { proxy in
                Color.clear
                    .onChange(of: proxy.size) { newValue in
                        contentHeight = newValue.height
                    }
            }
        )
Sign up to request clarification or add additional context in comments.

2 Comments

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?
Here you can save the height, to any property.
0

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
    }
)

1 Comment

its great, but how can I assign these to state variable.

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.