1

I'm currently working on a swiftUI project where I'm supposed to be able to interact with the content of a scrollView. The problem is that I want to scroll only using buttons. It works but I can't figure a way to disable the scrolling without disabling the content of my ScrollView

Here's my code. I know that disabled(true) won't work but it's what I got so far

            ScrollViewReader { scrolling in
            ZStack{
                HStack{
                    ScrollView(.horizontal, showsIndicators: false){
                        HStack{
                            FirstStepView().id(0).padding(.horizontal,20).frame(width: UIScreen.main.bounds.width)
                            SecondStepView().id(1).frame(width: UIScreen.main.bounds.width)
                            ThirdStepView().id(2).frame(width: UIScreen.main.bounds.width)
                            FourthStepView().id(3).frame(width: UIScreen.main.bounds.width)
                        }
                    }.disabled(true)
                    
                    .animation(
                        Animation.easeOut(duration: 1)
                    )
                }
                .onChange(of: myId, perform: { value in
                    withAnimation{scrolling.scrollTo(myId)}
                })
            }
        }
Hugo R
  • 25
  • 1
  • 9
  • 1
    You can use approach from the following solution (see section for SwiftUI 1.0) to capture underlying `UIScrollView` and set `isScrollEnabled = false` in it. https://stackoverflow.com/a/60855853/12299030. – Asperi Jan 22 '21 at 12:36

1 Answers1

4

Don't forget to provide a minimum reproducible example something we can copy and paste to ensure that the integrity if what you want is kept.

If your *StepView.count is static the code below might work for you

struct ButtonScroll: View {
    @State var myId: Int = 0
    
    init() {
        //Add this
        //It does NOT work with ScrollView
        UIScrollView.appearance().isScrollEnabled = false
    }
    var body: some View {
        VStack{
            //Simulates myId changing
            Button("change-position", action: {
                myId = Int.random(in: 0...3)
                print(myId.description)
            })
            ZStack{
                //TabView does not allow for changes in Tab/Page count nicely
                TabView(selection: $myId){
                    //Used Button to test interaction
                    Button("FirstStepView", action: {
                        print("FirstStepView")
                    }).tag(0)
                    Button("SecondStepView", action: {
                        print("SecondStepView")
                    }).tag(1)
                    Button("ThirdStepView", action: {
                        print("ThirdStepView")
                    }).tag(2)
                    Button("FourthStepView", action: {
                        print("FourthStepView")
                    }).tag(3)
                }.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
                .animation(
                    Animation.easeOut(duration: 1)
                )
            }
        }
    }
}
lorem ipsum
  • 21,175
  • 5
  • 24
  • 48