1

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?

Chiah Soon
  • 507
  • 5
  • 18

1 Answers1

1

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!

Chiah Soon
  • 507
  • 5
  • 18
  • 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. – Brett Nov 19 '20 at 03:01
  • 1
    No worries, thanks for updating. I noticed similar weird behaviour with buttons and scrollviews too. – Brett Nov 20 '20 at 00:21