Skip to content

ScrollViews

Nick Sarno edited this page Apr 8, 2022 · 1 revision

ScrollViewWithScrollToLocation

ScrollView with "scroll to location" capability. Developer is responsible for setting the .id(AnyHashable) on the content.

ScrollViewWithScrollToLocation(scrollToLocation: AnyHashable?) {
     VStack {
          ForEach(0..<100) { x in
               Text("\(x)")
                    .id(x)
               }
          }
     }
}

Customize animation with optional parameters

ScrollViewWithScrollToLocation(.vertical, showsIndicators: true, scrollToLocation: AnyHashable?, scrollAnchor: UnitPoint, animated: false) {
     VStack {
          ForEach(0..<100) { x in
               Text("\(x)")
                    .id(x)
               }
          }
     }
}

ScrollViewWithOnScrollChanged

ScrollView with "scrollViewDidScroll" capability. Returned frame is the location of the first pixel at the beginning of the ScrollView's content.

ScrollViewWithOnScrollChanged {
     VStack {
          ForEach(0..<100) { _ in
               Text("Hello, world!")
          }
     }
} onScrollChanged: { origin in
     let yOffset = origin.minY
     let xOffset = origin.minX
}
Clone this wiki locally