init(content: (ScrollViewProxy ) -> Content)
Creates an instance that can perform programmatic scrolling of its child scroll views.
Availability 有効性
Technology
@frozen struct ScrollViewReader<Content> where Content : View
The scroll view reader’s content view builder receives a Scroll
instance; you use the proxy’s scroll
to perform scrolling.
The following example creates a Scroll
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 Scroll
to scroll to the bottom button, and vice versa.
var topID
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)
}
Important 重要
You may not use the Scroll
during execution of the content
view 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 on
method.
init(content: (ScrollViewProxy ) -> Content)
var content: (ScrollViewProxy ) -> Content
var body: some View
typealias Body
struct ScrollView
struct ScrollViewProxy