I'm trying to make my ScrollView
:
Here's my code:
struct ContentView: View {
init() {
UIScrollView.appearance().alwaysBounceVertical = false
}
var body: some View {
ScrollView {
Rectangle()
.fill(Color.blue)
.frame(height: 300) /// is smaller than the screen
.padding()
}
}
}
I tried setting UIScrollView.appearance().alwaysBounceVertical = false
, but the scroll view still bounces:
If I do UIScrollView.appearance().bounces = false
, it stops bouncing. However, if I make the rectangle taller than the screen, it also stops bouncing (which I don't want).
How can I disable bouncing, but only when the content is smaller than scroll view's bounds?
From iOS 16.4 onwards, there is a new modifier for called .scrollBounceBehavior
that can be used to prevent ScrollView from bouncing when the content is smalled than the screen.
Example:
struct ContentView: View {
var body: some View {
ScrollView {
Rectangle()
.fill(Color.blue)
.frame(height: 300)
.padding()
}
.scrollBounceBehavior(.basedOnSize)
}
}