Recently I wrote a small SwiftUI library that can rotate an array of Views and select view.
But found an issue that I can't figure out how to disable scroll in ScrollView while in normal mode.
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.
setnestedscrollingenabled set it to false.
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 .
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.
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
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 : []
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With