user14870547
user14870547

Reputation:

Picker in SwiftUI Form turns gray after selecting a value

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

Upvotes: 5

Views: 1131

Answers (1)

Steven Craft
Steven Craft

Reputation: 1253

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

Upvotes: 5

Related Questions