<!--
{
  "availability" : [
    "iOS: 17.0.0 -",
    "iPadOS: 17.0.0 -",
    "macCatalyst: 17.0.0 -",
    "macOS: 14.0.0 -",
    "tvOS: 17.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/ScrollTargetBehavior",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI20ScrollTargetBehaviorP"
  },
  "title" : "ScrollTargetBehavior"
}
-->

# ScrollTargetBehavior

A type that defines the scroll behavior of a scrollable view.

```
protocol ScrollTargetBehavior
```

## Overview

A scrollable view calculates where scroll gestures should end using its
deceleration rate and the state of its scroll gesture by default. A scroll
behavior allows for customizing this logic.

You define a scroll behavior using the
[`updateTarget(_:context:)`](/documentation/SwiftUI/ScrollTargetBehavior/updateTarget(_:context:)) method.

Using this method, you can control where someone can scroll in a scrollable
view. For example, you can create a custom scroll behavior
that aligns to every 10 points by doing the following:

```
struct BasicScrollTargetBehavior: ScrollTargetBehavior {
    func updateTarget(_ target: inout ScrollTarget, context: TargetContext) {
        // Align to every 1/10 the size of the scroll view.
        let multiple = context.containerSize.width / 10.0
        let newX = (target.rect.origin.x / multiple).rounded() * multiple
        target.rect.origin.x = newX
    }
}
```

### Paging Behavior

SwiftUI offers built in scroll behaviors. One such behavior
is the [`PagingScrollTargetBehavior`](/documentation/SwiftUI/PagingScrollTargetBehavior) which uses the geometry of the scroll
view to decide where to allow scrolls to end.

In the following example, every view in the lazy stack is flexible
in both directions and the scroll view will settle to container aligned
boundaries.

```
ScrollView {
    LazyVStack(spacing: 0.0) {
        ForEach(items) { item in
            FullScreenItem(item)
        }
    }
}
.scrollTargetBehavior(.paging)
```

### View Aligned Behavior

SwiftUI also offers a [`ViewAlignedScrollTargetBehavior`](/documentation/SwiftUI/ViewAlignedScrollTargetBehavior) scroll behavior
that will always settle on the geometry of individual views.

```
ScrollView(.horizontal) {
    LazyHStack(spacing: 10.0) {
        ForEach(items) { item in
            ItemView(item)
        }
    }
    .scrollTargetLayout()
}
.scrollTargetBehavior(.viewAligned)
.safeAreaPadding(.horizontal, 20.0)
```

You configure which views should be used for settling using the
[`scrollTargetLayout(isEnabled:)`](/documentation/SwiftUI/View/scrollTargetLayout(isEnabled:)) modifier. Apply this modifier to a
layout container like [`LazyVStack`](/documentation/SwiftUI/LazyVStack) or [`HStack`](/documentation/SwiftUI/HStack) and each individual
view in that layout will be considered for alignment.

Use types conforming to this protocol with the
[`scrollTargetBehavior(_:)`](/documentation/SwiftUI/View/scrollTargetBehavior(_:)) modifier.

## Topics

### Getting the scroll target behavior

[`paging`](/documentation/SwiftUI/ScrollTargetBehavior/paging)

The scroll behavior that aligns scroll targets to container-based
geometry.

[`viewAligned`](/documentation/SwiftUI/ScrollTargetBehavior/viewAligned)

The scroll behavior that aligns scroll targets to view-based geometry.

[`viewAligned(limitBehavior:)`](/documentation/SwiftUI/ScrollTargetBehavior/viewAligned(limitBehavior:))

The scroll behavior that aligns scroll targets to view-based geometry.

### Updating the proposed target

[`updateTarget(_:context:)`](/documentation/SwiftUI/ScrollTargetBehavior/updateTarget(_:context:))

Updates the proposed target that a scrollable view should scroll to.

[`ScrollTargetBehavior.TargetContext`](/documentation/SwiftUI/ScrollTargetBehavior/TargetContext)

The context in which a scroll behavior updates the scroll target.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)