Home > Mobile >  SWIFTUI - How to disable scrolling up if users are already at the top of a ScrollView and vice versa
SWIFTUI - How to disable scrolling up if users are already at the top of a ScrollView and vice versa

Time:10-18

How the scroll view looks normally

What I DON'T WANT TO HAPPEN

As you can see from the images, I do not want to allow users to be able to scroll further up if they are already at the top, or scroll further down if they are already at the bottom.

What would be the best way to do this?

I would appreciate a detailed response. Thanks!

CodePudding user response:

Well first of all, that is default iOS behavior and disabling it will feel weird.

This is currently not supported directly in SwiftUI, but you can look through the view hierarchy to find the underlying UIScrollView and then disable the bounce on it.

The easiest way to do this is adding Introspect to your app and then

ScrollView {
    ...
}
.introspectScrollView { scrollView in
    scrollView.bounces = false
}

This will disable the bounce on the scrollview preventing a user from overscrolling

  • Related