<!--
{
  "availability" : [
    "iOS: 26.0.0 -",
    "iPadOS: 26.0.0 -",
    "macCatalyst: 26.0.0 -",
    "macOS: 26.0.0 -",
    "tvOS: 26.0.0 -",
    "visionOS: 26.0.0 -",
    "watchOS: 26.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/ScrollTargetBehavior/viewAligned(anchor:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI20ScrollTargetBehaviorPA2A011ViewAlignedcdE0VRszrlE04viewG06anchorAeA9UnitPointVSg_tFZ"
  },
  "title" : "viewAligned(anchor:)"
}
-->

# viewAligned(anchor:)

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

```
@export(implementation) static func viewAligned(anchor: UnitPoint?) -> Self
```

## Discussion

You use this behavior when a scroll view should always align its
scroll targets to a rectangle that’s aligned to the geometry of a view. In
the following example, the scroll view always picks an item view
to settle on.

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

You configure which views should be used for settling using the
`View/scrollTargetLayout()` 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.

You can customize whether the view aligned behavior limits the
number of views that can be scrolled at a time by using the
`ViewAlignedScrollTargetBehavior.LimitBehavior` type. Provide a value
of `ViewAlignedScrollTargetBehavior.LimitBehavior/always` to always
have the behavior only allow a few views to be scrolled at a time.

You can further customize how the view aligned behavior aligns the
view within the visible region of the scroll view by providing a custom
anchor. By default, the behavior will align the view to the top or
leading edge of the scroll view.

---

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)