这是我的列表视图:
struct ContentView: View {
let colors: [Color] = [.red, .green, .yellow, .orange, .blue, .black, .pink, .purple, .gray]
var body: some View {
NavigationView {
List {
ForEach(colors, id: \.self) { color in
NavigationLink(destination: DetailView(color: color)) {
Text(color.name!)
}
}
}
}
}
}
extension Color {
var name: String? {
switch self {
case Color.red: return "red"
case Color.green: return "green"
case Color.yellow: return "yellow"
case Color.blue: return "blue"
case Color.orange: return "orange"
case Color.black: return "black"
case Color.pink: return "pink"
case Color.purple: return "purple"
case Color.gray: return "gray"
default: return nil
}
}
}
和详细视图:
struct DetailView: View {
var color: Color
var body: some View {
Circle()
.foregroundColor(color)
}
}
当从详细视图返回时,行覆盖如下所示:
知道为什么会这样吗?