I have a swiftUI view that lists items. They have trailing swipe actions. I have given accessibilityIdentifiers to the button in the swipe menu. But the Xcode tool: Accessibility Inspector, does not get them first time.
Here is my view:
import SwiftUI
struct Person: Identifiable {
let id = UUID()
var name: String
var phoneNumber: String
}
var staff = [
Person(name: "Harry Potter", phoneNumber: "(408) 555-4301"),
Person(name: "Ronald Weasley", phoneNumber: "(919) 555-2481")
]
struct ContentView: View {
var body: some View {
List {
ForEach(staff) { person in
Cell(title: person.name)
}
}
}
}
struct Cell: View {
var title: String
var body: some View {
Text(title)
.swipeActions {
swipeMenu
}
}
@ViewBuilder var swipeMenu: some View {
Button {
} label: {
Label("Logout", systemImage: "logout")
}
.tint(.blue)
.accessibilityIdentifier("person-logout")
Button {
} label: {
Label("Edit", systemImage: "edit")
}
.tint(.green)
.accessibilityIdentifier("person-edit")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Steps:
- Open the app.
- Swipe first row. "Edit" and "Logout" actions become visible.
- Launch the Accessibility inspector.
- Inspect the UI. The Identifiers for "Edit" and "Logout" button are not captured by the inspector.
- Swipe the row to hide the actions.
- Now swipe the row again to view the actions.
- Inspect the UI elements. This time the Accessibility inspector is able to capture the identifiers.
Why is this happening? How do we get the Accessibility Inspector to capture the identifiers the first time?