SwiftUI如何在多级NavigationView中停止List的动画

时间:2020-01-16 18:11:18

标签: swiftui navigationlink

当我在多级NavigationView中有一个多列表时,我只想停止动画。也许这不是“动画”,我只是想解决这个问题。

在Xcode版本11.3.1(11C504)+ iOS 13.2上

enter image description here

代码很简单,您可以找到已连接的代码。

import SwiftUI

struct TestView: View {
    var body: some View {
        NavigationView {
            List {
                ForEach(1...4, id: \.self) {_ in
                    NavigationLink(destination: AView()) {
                        Text("root")
                    }
                }
            }
        }
    }
}

struct AView: View {
    var body: some View {
        List {
            ForEach(1...4, id: \.self) {_ in
                NavigationLink(destination: BView()) {
                    Text("aview")
                }
            }
        }
    }
}

struct BView: View {
    var body: some View {
        List {
            ForEach(1...4, id: \.self) {_ in
                NavigationLink(destination: BView()) {
                    Text("bview")
                }
            }
        }
    }
}

struct TestView_Previews: PreviewProvider {
    static var previews: some View {
        TestView()
    }
}

1 个答案:

答案 0 :(得分:2)

好吧...我已经安装了运气不好的Xcode 11.3.1 ...,这是一个解决方案(无论如何都可以解决)-使用显式.listRowInsets,如以下示例所示(顺便说一句,可以是任何值)

List {
    ForEach(1...1000, id: \.self) {_ in
        NavigationLink(destination: BView()) {
            Text("bview")
        }
    }
    .listRowInsets(EdgeInsets(top: 0, leading: 20, bottom: 0, trailing: 20))
}

适用于任何动态列表