SwiftUI Segmented Picker element is grey and disabled

Viewed 663

I've added a Picker (SegmentedPickerStyle) to my project using SwiftUI as shown:

Picker(selection: $selectedType, label: Text("Type")) {
      Text("Translation")
      Text("Highlights")
}
.pickerStyle(SegmentedPickerStyle())

But for some reason, the result (both in canvas and the simulator) is a grey, disabled picker (please see screenshot attached).

Image

What I try to achieve: Image 2 Does anyone has an idea what am I doing wrong?

2 Answers

Try as following

@State private var selectedType = 0

...

Picker(selection: $selectedType, label: Text("Type")) {
      Text("Translation").tag(0)
      Text("Highlights").tag(1)
}
.pickerStyle(SegmentedPickerStyle())

Your picker has 2 Text items, "Translation" & "Highlights". So your selection will be either of those Strings. Create a @State variable to track when that selection changes and to show the new 'State'.

@State private var selectedType = "Translation"
...
Picker(selection: $selectedType, label: Text("Type")) {
      Text("Translation")
      Text("Highlights")
}
.pickerStyle(SegmentedPickerStyle())
Related