Crash when using hiding a ProgressView in a Picker label

Viewed 83

I am trying to put a ProgressView inside a Picker label. When I tap the Hide Spinner button, this (intermittently) crashes with EXC_BAD_ACCESS (code=EXC_I386_GPFLT).

struct ContentView: View {
    @State private var selectedCity = ""
    @State private var showSpinner = true
    
    let cities = [
        "Calgary",
        "Edmonton",
        "Toronto"
    ]
    
    var body: some View {
        NavigationView {
            VStack(spacing: 0) {
                Form {
                    Picker(selection: $selectedCity, label:
                            HStack {
                                Text("Your City")
                                if showSpinner {
                                    ProgressView()
                                        .padding(.horizontal, 2)
                                }
                            }
                    ) {
                        ForEach(cities, id: \.self) { city in
                            Text(city).tag(city)
                        }
                    }
                    
                    Button("Hide Spinner", action: { showSpinner = false })
                }
            }
            .navigationBarTitle("ProgressView Crash", displayMode: .inline)
        }
    }
}

Am I doing anything wrong? I'm guessing this is a SwiftUI bug. I get the same behaviour when wrapping a UIActivityIndicatorView in a UIViewRepresentable.

1 Answers

Yes, it looks like a bug with auto-generated accessibility label. The safe workaround is to use explicitly provided accessibility.

Tested with Xcode 12 / iOS 14

Picker(selection: $selectedCity, label:
        HStack {
            Text("Your City")
            if showSpinner {
                ProgressView()
                    .padding(.horizontal, 2)
            }
        }.accessibility(label: Text("Your City"))      // << here !!
) {
    ForEach(cities, id: \.self) { city in
        Text(city).tag(city)
    }
}
Related