Enable scrolling based on content size in SwiftUI
When designing interfaces in SwiftUI, it’s often a good idea to wrap our app’s content in a scroll view, even if the content usually fits on screen. This helps ensure that users who enable larger text sizes in accessibility settings can still access all the content without layout issues, clipped views and truncated text. However, doing this introduces an unintended side effect. By default, ScrollView
adds a bouncy scrolling behavior, even when the content fits entirely within the available space. This can make the interface feel oddly springy when no scrolling is actually needed.
To address this, we can use the scrollBounceBehavior(_:axes:) modifier introduced in iOS 16.4. When we apply this modifier to a ScrollView
or to a view hierarchy that contains one, and pass the basedOnSize
value, SwiftUI automatically disables bounce behavior when the content fits and enables it only when scrolling is actually required.
ScrollView {
WalkDetailView()
}
.scrollBounceBehavior(.basedOnSize)
This small adjustment helps create a more polished experience, adapting gracefully to both default and accessibility text sizes.