scrollviewswiftuibounce

How to disable ScrollView Bounce In SwiftUI


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")
       ...
       ...
}

Solution

  • 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
     }