Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios SwiftUI-ScrollViewReader';s scrollTo不滚动_Ios_Swift_Swiftui_Scrollview - Fatal编程技术网

Ios SwiftUI-ScrollViewReader';s scrollTo不滚动

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

我有一个简单的SwiftUI列表,当用户单击按钮时,我想滚动到一行。我已从中复制了此代码。它本该起作用,但却不行

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)
                    }
                }
            }
        }
    }
}