Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to only disable scroll in ScrollView but not content view?

Tags:

ios

swift

swiftui

Recently I wrote a small SwiftUI library that can rotate an array of Views and select view.

Gif-1

But found an issue that I can't figure out how to disable scroll in ScrollView while in normal mode.

Gif-2

I tried to put .disabled(true) at the end of the ScrollView unfortunately, it not only disable scroll but also all the views in ScrollView.

Here's source code of the project.

What modifier should I add to solve this?

--Edited--

I have tried to change the scroll axis but once it becomes [], scrollview will reset its content offset, wondering if there's a way to block scrolling without changing the axis.

--Solved--

At last, I just add a DragGesture() to block scroll event and works fine.

like image 519
raxabizze Avatar asked Sep 14 '19 08:09

raxabizze


People also ask

How do I stop Nestedscrollview scrolling?

setnestedscrollingenabled set it to false.

How do I stop a scroll event?

We can't prevent scrolling by using event. preventDefault() in onscroll listener, because it triggers after the scroll has already happened. But we can prevent scrolling by event. preventDefault() on an event that causes the scroll, for instance keydown event for pageUp and pageDown .

How do I disable scroll overflow?

To hide the scrollbar and disable scrolling, we can use the CSS overflow property. This property determines what to do with content that extends beyond the boundaries of its container. To prevent scrolling with this property, just apply the rule overflow: hidden to the body (for the entire page) or a container element.


2 Answers

I think you can use

.simultaneousGesture(DragGesture(minimumDistance: 0), including: .all)

It disables scroll in ScrollView but other gestures still work like tapGesture inside this scrollview, and ScrollViewReader also works

like image 97
Michał Ziobro Avatar answered Oct 31 '22 01:10

Michał Ziobro


Only pass .horizontal as the scroll axis if you want the view to scroll. Otherwise, pass the empty set.

struct TestView: View {
    @Binding var shouldScroll: Bool

    var body: some View {
        ScrollView(axes, showsIndicators: false) {
            Text("Your content here")
        }
    }

    private var axes: Axis.Set {
        return shouldScroll ? .horizontal : []
    }
}
like image 41
rob mayoff Avatar answered Oct 31 '22 01:10

rob mayoff