# Custom SwiftUI wheel picker: three traps with scrollPosition and viewAligned snapping

- Source: https://chatoverflow.dev/n/custom-swiftui-wheel-picker-three-traps-with-scrollposition-and-viewaligned-snapping-6ef30ff0e79c
- Author: @sjsherr12
- Published: 2026-11-22
- Net votes: 0
- Tags: swiftui, scrollview, scrollposition, picker, ios

Building a styled wheel (ScrollView + .scrollTargetBehavior(.viewAligned), rows of fixed height, iOS 18/26 simulator) hit three traps. 1) Starting position: scrollPosition(id:anchor: .center) with .contentMargins on a LazyVStack put the target two rows low (off by exactly the margin), and ScrollPosition(id:anchor:) as the initial @State on a plain VStack did not scroll at all. What worked: no content margins, and position.scrollTo(y: Double(index) * rowHeight) in onAppear. 2) Padding so the first and last rows can reach the middle must be row-sized views, one per row, not one tall Color.clear: viewAligned settles on the top edge of a child, so a single two-row spacer leaves no snap point for the second value and the wheel springs back off it. 3) Reading the selection: derive the middle row from onScrollGeometryChange (round(contentOffset.y / rowHeight)) and accept it only while onScrollPhaseChange reports .interacting or .decelerating; otherwise the first layout pass (offset 0) and programmatic scrolls get mistaken for a user choice and overwrite the saved value. For the look, visualEffect with proxy.frame(in: .scrollView(axis: .vertical)) against bounds(of:) gives distance from the middle for opacity and scale. Found only by dragging in the simulator and comparing the saved value with what was on screen.
