Duck
Duck

Reputation: 35933

Picker appears disabled inside a Form... bug?

Consider this code:

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

}

Run it. The result is the picker disabled. Why? How to I enable it?

The problem is the picker being inside a form. Section makes no difference.

enter image description here

Upvotes: 2

Views: 508

Answers (1)

Jignesh Mayani
Jignesh Mayani

Reputation: 7193

You have to add Form in Navigation View

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

Upvotes: 4

Related Questions