ScrollViewReader
A view that provides programmatic scrolling, by working with a proxy to scroll to known child views.
Declaration
import { ScrollViewReader } from 'swiftui-react-native';@frozen nonisolated struct ScrollViewReader<Content> where Content : ViewInitializers
One SwiftUI initializer backs <ScrollViewReader>.
<ScrollViewReader>
children?: ReactNode | ((arg0: SwiftUI.ScrollViewProxy) => ReactNode)
</ScrollViewReader>nonisolated init(
@ContentBuilder content: @escaping (ScrollViewProxy) -> Content
)childrenReactNode | ((arg0: SwiftUI.ScrollViewProxy) => ReactNode)Swift
content: (ScrollViewProxy) -> Content
Also takes the common view props: modifiers, style and testID.
Overview
The scroll view reader's content content builder receives a ScrollViewProxy
instance; you use the proxy's ScrollViewProxy.scrollTo(_:anchor:) to
perform scrolling.
The following example creates a ScrollView containing 100 views that
together display a color gradient. It also contains two buttons, one each
at the top and bottom. The top button tells the ScrollViewProxy to
scroll to the bottom button, and vice versa.
@Namespace var topID
@Namespace var bottomID
var body: some View {
ScrollViewReader { proxy in
ScrollView {
Button("Scroll to Bottom") {
withAnimation {
proxy.scrollTo(bottomID)
}
}
.id(topID)
VStack(spacing: 0) {
ForEach(0..<100) { i in
color(fraction: Double(i) / 100)
.frame(height: 32)
}
}
Button("Top") {
withAnimation {
proxy.scrollTo(topID)
}
}
.id(bottomID)
}
}
}
func color(fraction: Double) -> Color {
Color(red: fraction, green: 1 - fraction, blue: 0.5)
}during execution of the content content builder; doing so results in a
runtime error. Instead, only actions created within content can call
the proxy, such as gesture handlers or a view's onChange(of:perform:)
method.
From Apple’s SwiftUI documentation. Code samples there are Swift.
Relationships
Conforms to
- View
See also
More in Layout.
- AnyLayoutA type-erased instance of the layout protocol.
- GeometryReaderA container view that defines its content as a function of its own size and coordinate space.
- GlassEffectContainerA view that combines multiple glass shapes into a single shape that can morph individual shapes into one another.
- GridA container view that arranges other views in a two dimensional layout.
- GridLayoutA grid that you can use in conditional layouts.
- GridRowA horizontal row in a two dimensional grid container.
- HStackA view that arranges its subviews in a horizontal line.
- HStackLayoutA horizontal container that you can use in conditional layouts.
- LazyHGridA container view that arranges its child views in a grid that grows horizontally, creating items only as needed.
- LazyHStackA view that arranges its children in a line that grows horizontally, creating items only as needed.
- LazyVGridA container view that arranges its child views in a grid that grows vertically, creating items only as needed.
- LazyVStackA view that arranges its children in a line that grows vertically, creating items only as needed.