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:

  1. Open the app. enter image description here
  2. Swipe first row. "Edit" and "Logout" actions become visible. enter image description here
  3. Launch the Accessibility inspector.
  4. Inspect the UI. The Identifiers for "Edit" and "Logout" button are not captured by the inspector. enter image description here
  5. Swipe the row to hide the actions.
  6. Now swipe the row again to view the actions.
  7. Inspect the UI elements. This time the Accessibility inspector is able to capture the identifiers. enter image description here

Why is this happening? How do we get the Accessibility Inspector to capture the identifiers the first time?

0

There are 0 best solutions below