Swiftui 选择器在窗体中显示为禁用状态。。。缺陷?
考虑以下代码:Swiftui 选择器在窗体中显示为禁用状态。。。缺陷?,swiftui,Swiftui,考虑以下代码: struct ContentView: View { var colors = ["Red", "Green", "Blue", "Tartan"] @State private var selectedColor = "Red" var body: some View { Form { Section (header:Text("color")
struct ContentView: View {
var colors = ["Red", "Green", "Blue", "Tartan"]
@State private var selectedColor = "Red"
var body: some View {
Form {
Section (header:Text("color")) {
Picker("Please choose a color", selection: $selectedColor) {
ForEach(colors, id: \.self) {
Text($0)
}
}
}
}
}
}
运行它。结果是选择器被禁用。为什么?如何启用它
问题是选择器在表单中。第二节没有区别
您必须在导航视图中添加表单
struct ContentView: View {
var colors = ["Red", "Green", "Blue", "Tartan"]
@State private var selectedColor = "Red"
var body: some View {
NavigationView {
Form {
Section (header:Text("color")) {
Picker("Please choose a color", selection: $selectedColor) {
ForEach(colors, id: \.self) {
Text($0)
}
}
}
}
.navigationBarTitle("Color Picker")
}
}
}
您必须在导航视图中添加表单
struct ContentView: View {
var colors = ["Red", "Green", "Blue", "Tartan"]
@State private var selectedColor = "Red"
var body: some View {
NavigationView {
Form {
Section (header:Text("color")) {
Picker("Please choose a color", selection: $selectedColor) {
ForEach(colors, id: \.self) {
Text($0)
}
}
}
}
.navigationBarTitle("Color Picker")
}
}
}