Ios SwiftUI-ScrollViewReader';s scrollTo不滚动
我有一个简单的SwiftUI列表,当用户单击按钮时,我想滚动到一行。我已从中复制了此代码。它本该起作用,但却不行Ios SwiftUI-ScrollViewReader';s scrollTo不滚动,ios,swift,swiftui,scrollview,Ios,Swift,Swiftui,Scrollview,我有一个简单的SwiftUI列表,当用户单击按钮时,我想滚动到一行。我已从中复制了此代码。它本该起作用,但却不行 struct ContentView: View { var body: some View { ScrollViewReader { proxy in VStack { Button("Jump to #50") { proxy.scrollT
struct ContentView: View {
var body: some View {
ScrollViewReader { proxy in
VStack {
Button("Jump to #50") {
proxy.scrollTo(5, anchor: .top)
}
List(0..<100) { i in
Text("Example \(i)")
.id(i)
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct ContentView:View{
var body:一些观点{
ScrollViewReader{中的代理
VStack{
按钮(“跳转到#50”){
proxy.scrollTo(5,锚点:。顶部)
}
列表(0..struct ContentView:View{
var body:一些观点{
ScrollViewReader{中的代理
VStack{
按钮(“跳转到#50”){
proxy.scrollTo(50,锚点:。顶部)
}
名单{
ForEach(0..ScrollViewReader仅适用于:
- 显式使用滚动视图
- 可识别收藏清单
除非您明确设置id,否则它不适用于范围的列表
显式设置id。
// List(0..<100, id: \.self)
struct ContentView: View {
var body: some View {
ScrollViewReader { proxy in
VStack {
Button("Jump to #50") {
proxy.scrollTo(5, anchor: .top)
}
List(0..<100, id: \.self) { i in
Text("Example \(i)")
.id(i)
}
}
}
}
}
// ForEach(0..<50000, id: \.self)
struct ContentView: View {
var body: some View {
ScrollView {
ScrollViewReader { proxy in
LazyVStack {
ForEach(0..<50000, id: \.self) { i in
Button("Jump to \(i+500)") {
proxy.scrollTo(i+500, anchor: .top)
}
Text("Example \(i)")
.id(i)
}
}
}
}
}
}
//List(0..Hmm…适用于iOS 14.1。我发现在14.2 SwiftUI中报告了太多的缺陷。。。
// List(0..<100, id: \.self)
struct ContentView: View {
var body: some View {
ScrollViewReader { proxy in
VStack {
Button("Jump to #50") {
proxy.scrollTo(5, anchor: .top)
}
List(0..<100, id: \.self) { i in
Text("Example \(i)")
.id(i)
}
}
}
}
}
// ForEach(0..<50000, id: \.self)
struct ContentView: View {
var body: some View {
ScrollView {
ScrollViewReader { proxy in
LazyVStack {
ForEach(0..<50000, id: \.self) { i in
Button("Jump to \(i+500)") {
proxy.scrollTo(i+500, anchor: .top)
}
Text("Example \(i)")
.id(i)
}
}
}
}
}
}