How it works
ScrollInputBehavior is a set of constants that describe how a scroll view should react to a particular class of input, such as a pointer, trackpad, or other scrolling device. You use it together with the scrollInputBehavior(_:for:) modifier to tell SwiftUI whether a given input is allowed to drive scrolling or should be ignored. Reach for it when the default behavior interferes with your design — for example, when you want a region to stay fixed under one kind of input while remaining interactive in other ways. Rather than intercepting and consuming gestures yourself, you declare the intent and let the scroll view enforce it.
Choose a behavior constant
ScrollInputBehavior is a struct that vends type properties representing the available responses to input. Select the constant that matches the effect you want — the example passes
.disabledto turn the behavior off for the targeted input. An.enabledconstant restores the standard response, so you can toggle behavior conditionally without restructuring the view.Apply it with scrollInputBehavior(_:for:)
You install a ScrollInputBehavior by attaching the
scrollInputBehavior(_:for:)modifier to a scrollable container. The modifier takes the behavior as its first argument and the input category asfor:, scoping the rule precisely. In the example it is applied directly to theScrollView, so the chosen behavior governs that view's response.Target an input category
The
for:parameter identifies which kind of input the behavior applies to, leaving every other input untouched. The example targets.scroll, meaning scroll-wheel and trackpad scrolling input specifically — combined with.disabled, this makes theScrollViewignore that input while other interaction paths remain intact.Scope it to the right view in the hierarchy
Because scrollInputBehavior(_:for:) is a view modifier, the ScrollInputBehavior you supply only affects the scroll view it is attached to and its scrollable descendants. Placing it on the outer
ScrollViewhere keeps the rule local to that container, so the surroundingVStackandForEachcontent layout exactly as before — only the scroll view's reaction to the named input changes.
.scrollInputBehavior(.disabled, for: .scroll) to .scrollInputBehavior(.enabled, for: .scroll) and compare how the view responds to scroll input.Example & preview
Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.
struct ScrollInputBehaviorDemo: View {
var body: some View {
ScrollView {
VStack(spacing: 12) {
ForEach(1..<11) { i in
Text("Row \(i)")
.frame(maxWidth: .infinity)
.padding()
.background(.blue.opacity(0.15))
.cornerRadius(8)
}
}
.padding()
}
.scrollInputBehavior(.disabled, for: .scroll)
}
}