使用在Picker标签中隐藏ProgressView时崩溃

时间:2020-09-29 21:31:42

标签: swiftui

我试图将ProgressView放在Picker标签内。当我点击“隐藏微调器”按钮时,这会间歇性地崩溃,并显示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)
        }
    }
}

我做错什么了吗?我猜这是一个SwiftUI错误。将UIActivityIndicatorView包裹在UIViewRepresentable中时,我得到相同的行为。

1 个答案:

答案 0 :(得分:2)

是的,它看起来像带有自动生成的辅助功能标签的错误。安全的解决方法是使用显式提供的可访问性。

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