I have a straightforward picker in a view. The picker is menu styled and works like expected, i.e. you can select the colors from the picker menu.
But when I add a button to my view that triggers a subview and return from this subview to the content view then the picker doesn't work anymore. It seems to freeze.
I know that the problem does not occur if I embed the content view in a NavigationView with a Form but then the picker choices are presented on a different view, which is not what I want. The problem only seems to appear with the menu picker style. Other picker styles don't freeze.
What am I doing wrong?
struct ContentView: View {
let colors: [String] = ["red", "white", "blue"]
@State private var selectedColor: String = "red"
@State private var showSubview: Bool = false
var body: some View {
Picker("Select color", selection: $selectedColor) {
ForEach(colors, id:\.self) { color in
Text(color)
}
}
Text("Selected color: \(selectedColor)")
Button(action: { showSubview = true }
) {
Text("Show subview")
}
.padding()
.sheet(isPresented: $showSubview) {
Subview()
}
}
}
struct Subview: View {
@Environment(\.presentationMode) private var presentationMode
var body: some View {
Button(action: {
presentationMode.wrappedValue.dismiss() }
) {
Text("Dismiss")
}
}
}