Picker in SwiftUI Form turns gray after selecting a value

Viewed 749

For some reason the picker in my SwiftUI Form turns gray after I select a value.

My code:

Form {
  Section {
    TextField("title", text: $title)
      Picker(selection: $category, label: Text("category")) {
        ForEach(0..<categories.count) { index in
            Text(categories[index]).tag(index)
      }
    }
  }
}

Here is how it looks on a iPhone 12 simulator

1 Answers

You need to wrap your form inside a NavigationView, the reason being when you tap the picker it will bring up another page, so it needs the navigation view in order to be able to navigate back.

NavigationView {
  Form {
    Section {
      TextField("title", text: $title)
        Picker(selection: $category, label: Text("category")) {
          ForEach(0..<categories.count) { index in
              Text(categories[index]).tag(index)
        }
      }
    }
  }
}
Related