<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: 18.0.0 -",
    "macOS: 15.0.0 -",
    "tvOS: 18.0.0 -",
    "visionOS: 2.0.0 -",
    "watchOS: 11.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/defaultScrollAnchor(_:for:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE19defaultScrollAnchor_3forQrAA9UnitPointVSg_AA0eF4RoleVtF"
  },
  "title" : "defaultScrollAnchor(_:for:)"
}
-->

# defaultScrollAnchor(_:for:)

Associates an anchor to control the position of a scroll view in a
particular circumstance.

```
nonisolated func defaultScrollAnchor(_ anchor: UnitPoint?, for role: ScrollAnchorRole) -> some View
```

## Discussion

You can associate a [`UnitPoint`](/documentation/SwiftUI/UnitPoint) to a [`ScrollView`](/documentation/SwiftUI/ScrollView) using the
[`defaultScrollAnchor(_:)`](/documentation/SwiftUI/View/defaultScrollAnchor(_:)) modifier. By default, the system
uses this point for different kinds of behaviors including:

- Where the scroll view should initially be scrolled
- How the scroll view should handle content size or
  container size changes
- How the scroll view should align content smaller than
  its container size

You can further customize this behavior by assigning different unit
points for these different cases.

For example, you can use the [`defaultScrollAnchor(_:)`](/documentation/SwiftUI/View/defaultScrollAnchor(_:)) modifier
to provide a value of [`bottom`](/documentation/SwiftUI/UnitPoint/bottom) as the anchor for all cases
and then opt out of certain cases by providing a different value
for them.

```
@Binding var items: [Item]
@Binding var scrolledID: Item.ID?

ScrollView {
    LazyVStack {
        ForEach(items) { item in
            ItemView(item)
        }
    }
}
.defaultScrollAnchor(.bottom)
.defaultScrollAnchor(.topLeading, for: .alignment)
```

---

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)