TechnologiesSwiftUIScrolling

ScrollInputBehavior struct

iOSmacOStvOSwatchOSvisionOSiOS 18.0+✓ renders

A type that defines whether input should scroll a view.

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.

  1. 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 .disabled to turn the behavior off for the targeted input. An .enabled constant restores the standard response, so you can toggle behavior conditionally without restructuring the view.

  2. 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 as for:, scoping the rule precisely. In the example it is applied directly to the ScrollView, so the chosen behavior governs that view's response.

  3. 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 the ScrollView ignore that input while other interaction paths remain intact.

  4. 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 ScrollView here keeps the rule local to that container, so the surrounding VStack and ForEach content layout exactly as before — only the scroll view's reaction to the named input changes.

Try it — Change .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.

ScrollInputBehavior.swift
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)
    }
}
Live preview
Row 1 Row 2 Row 3 Row 4 Row 5 Row 6 Row 7 Row 8 Row 9 Row 10
Row 1 Row 2 Row 3 Row 4 Row 5 Row 6 Row 7 Row 8 Row 9 Row 10
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →