22

Any Modifier available to stop bounce of ScrollView in swiftUI ?

struct RoomDetailsView: View {

    var body: some View {
        ScrollView(showsIndicators: false) {
            Image("test")
            Text("Hello Text")
            ...
            ...
        }
    }
}

I tried below code but it not work for me. looks like it deprecated

ScrollView(alwaysBounceVertical: true) {
       Image("test")
       Text("Hello Text")
       ...
       ...
}
Rohit Makwana
  • 3,668
  • 1
  • 19
  • 27
  • hmm.. I don't observe any bounce in the described scenario. So do you want to "stop" or to "add", because in second block of code it seems you try to enable it, however in question to disable it. – Asperi Nov 11 '19 at 13:30
  • I want to stop bounce in scrollview. earlier second block is working. but now I'm not found right now any solution for that my  current code is looks like first block. – Rohit Makwana Nov 12 '19 at 05:17
  • 2
    Did you find any solution? – Sandeep Maurya Dec 06 '19 at 07:31
  • not able to find solution – Rohit Makwana Jan 06 '20 at 09:30
  • 2
    For the case of static content inside ScrollView it can be used solution from [SwiftUI: Make ScrollView scrollable only if it exceeds the height of the screen](https://stackoverflow.com/a/62466397/12299030). It is not applicable for active content, because based on `.disabled` modifier. – Asperi Jun 19 '20 at 08:57

2 Answers2

23

try using this line of code:

UIScrollView.appearance().bounces = false

You can use it like this:-

struct RoomDetailsView: View {
   init() {
      UIScrollView.appearance().bounces = false
   }

   var body: some View {
      ScrollView(showsIndicators: false) {
         Image("test")
         Text("Hello Text")
         ...
         ...
          }
      }
  }

Or you can write this line in AppDelegate to apply this behaviour throughout into your app.

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    UIScrollView.appearance().bounces = false
 }
Anshuman Singh
  • 838
  • 12
  • 17
  • 7
    doesnt the first one change all scrollviews in my app too? – Di_Nerd May 29 '20 at 09:29
  • 1
    Yup, @DiNerd the first will eventually change the appearance throughout the application, and this is the actual statement, below ones are just its implementation example. You can use any of the below two ways to achieve it. – Anshuman Singh Jun 01 '20 at 06:00
  • If you only want to disable bouncing if the content is smaller than the scrollview size, do this: `UIScrollView.appearance().alwaysBounceVertical = false` – Tamás Sengel Mar 05 '22 at 15:59
5

You may use SwiftUI-Introspect library:

ScrollView {
    // some content
}
.introspectScrollView { scrollView in
    scrollView.alwaysBounceVertical = false
}
General Failure
  • 2,393
  • 4
  • 20
  • 45