Picker freezes

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")
        }
    }
}
Answered by Claude31 in 699034022

I tried a different solution, to no avail, same problem.

It seems that sheet dismissing is the cause.

struct ContentView11: 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(isPresented: $showSubview)
        }
    }
}

struct Subview: View {
    /*
    @Environment(\.presentationMode) private var presentationMode
    
    var body: some View {
        Button(action: {
            presentationMode.wrappedValue.dismiss() }
        ) {
            Text("Dismiss")
        }
    }
     */
    @Binding var isPresented: Bool

    var body: some View {
        Button("Dismiss Me") {
            isPresented = false
        }
    }

}

This older thread may be of interest:

https://developer.apple.com/forums/thread/131404

Likely, something not working in SwiftUI for sheet.

Should file a bug report.

Accepted Answer

I tried a different solution, to no avail, same problem.

It seems that sheet dismissing is the cause.

struct ContentView11: 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(isPresented: $showSubview)
        }
    }
}

struct Subview: View {
    /*
    @Environment(\.presentationMode) private var presentationMode
    
    var body: some View {
        Button(action: {
            presentationMode.wrappedValue.dismiss() }
        ) {
            Text("Dismiss")
        }
    }
     */
    @Binding var isPresented: Bool

    var body: some View {
        Button("Dismiss Me") {
            isPresented = false
        }
    }

}

This older thread may be of interest:

https://developer.apple.com/forums/thread/131404

Likely, something not working in SwiftUI for sheet.

Should file a bug report.

Picker freezes
 
 
Q