Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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")
       ...
       ...
}
like image 215
Rohit Makwana Avatar asked Nov 11 '19 10:11

Rohit Makwana


People also ask

How do I stop bounce ScrollView in SwiftUI?

The Answers struct RoomDetailsView: View { init() { UIScrollView. appearance(). bounces = false } var body: some View { ScrollView(showsIndicators: false) { Image("test") Text("Hello Text") ... ... } } }

What is ScrollView in SwiftUI?

Figure 1. The ScrollView of SwiftUI allows efficient creation of scrolling containers. The view automatically adjust its size to fit the placed objects inside. It will also place some adjustments in order to avoid safe areas. ScrollView can be scrolled horizontally, vertically, or both.


2 Answers

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
 }
like image 124
Anshuman Singh Avatar answered Oct 19 '22 02:10

Anshuman Singh


You may use SwiftUI-Introspect library:

ScrollView {
    // some content
}
.introspectScrollView { scrollView in
    scrollView.alwaysBounceVertical = false
}
like image 5
General Failure Avatar answered Oct 19 '22 02:10

General Failure