2

For the past 2 hours, I have been trying to get a ScrollView of buttons to work, more specifically ScrollView of buttons that uses GeometryReader to determine its size. Additionally, all this is placed within a NavigationView.

There was weird behaviour like the buttons not performing its action, not registering the tap, and when placed in HStack within a VStack for grid-like structure, I could only tap the first row of buttons with the intended effects, the other buttons were all unresponsive. How do I structure all of these views properly?

1 Answer 1

2

The code below worked for me:

    var body : some View {
        NavigationView {
            VStack{
                GeometryReader{ geometry in
                    ScrollView {
                      // This is just how i'm setting up my buttons
                        VStack {
                            ForEach(0 ..< 2, id: \.self) { row in
                                HStack {
                                    ForEach(0 ..< 2, id: \.self) { column in
                                        TestButton()
                                    }
                                }
                            }
                        }
                    }
                }
            }.navigationBarTitle("")
            .navigationBarHidden(true)
        }
        .navigationViewStyle(StackNavigationViewStyle())
        .edgesIgnoringSafeArea(.all)
        .navigationBarTitle("")
        .navigationBarHidden(true)
    }

Note that the ScrollView has to be nested within the GeometryReader, if not there would be weird behavior (in my case, I could only tap the first line of buttons with the intended effects). Hope this helps someone!

Sign up to request clarification or add additional context in comments.

2 Comments

Your code does not match the comment. Can you fix the code or the comment? Your GeometryReader is NOT nested within the ScrollView in the above code.
No worries, thanks for updating. I noticed similar weird behaviour with buttons and scrollviews too.

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.