UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.