TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Is this a bug? Changing ScrollView Axis.Set doesn't change bounce.

Forums > SwiftUI

I have a ScrollView that I want to be able to control whether it is scrollable or not, so I set it's Axis.Set to be dependent on an @State variable, like so:

struct ContentView: View {
    @State private var scrollable = false

    var body: some View {
        VStack {
            Text("scrollable: \(scrollable.description)")
                .onTapGesture {
                    scrollable.toggle()
                }

            ScrollView(scrollable ? [.horizontal, .vertical] : []) {
                Circle()
                    .frame(width: 300, height: 300)
            }.frame(width: 200, height: 200).border(Color.red)

        }
    }
}

It starts out with with an empty Array and that works as expected - swipe gestures do not move the circle around. When I toggle scrollable to true, that makes the ScrollView scrollable - again, as expected. But once you set it back again, you can never get rid of that bounce. Is this a bug?

One solution I thought of was to use .disabled with an @State boolean on the ScrollView itself, but that cascades down to the Child Views, and I need those to be user interactive.

Any help would be greatly appreciated.

3      

I have a ScrollView that I want to be able to control whether it is scrollable or not ...

@State
    private var isScrollable = false
    var body: some View {
        VStack {
            ScrollView {
                ForEach(0..<11) {
                    Text("Item: \($0)")
                        .padding(.vertical, 10)
                        .padding(.horizontal, 55)
                        .background(Color.secondary.opacity(0.35))
                        .clipShape(RoundedRectangle(cornerRadius: 15, style: .continuous))
                }
            }
            .frame(width: 200, height: 200).border(Color.red)
            .disabled(isScrollable)
            .padding()

            Button(action: { isScrollable.toggle()},
                   label: {
                    Text(isScrollable ? "disabled" : "scrollable")
                        .font(.title)
                        .bold()
                        .foregroundColor(.white)
                        .padding()
                        .background(isScrollable ? Color.red : .blue)
                        .clipShape(Capsule(style: .continuous))
                   })
        }
    }

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.