Post

Replies

Boosts

Views

Activity

Reply to SwiftUI: Adding accessibility identifiers to VStack
The solution for me was to create an un-styled GroupBox to "wrap" any content inside. After that we can assign an accessibilityIdentifier to it. It doesn't disrupt the screen readers or change the layout. Code: /// Groupbox "no-op" container style without label /// to combine the elements for accessibilityId struct ContainerGroupBoxStyle: GroupBoxStyle { func makeBody(configuration: Configuration) -> some View { configuration.content } } extension GroupBoxStyle where Self == ContainerGroupBoxStyle { static var contain: Self { Self() } } extension View { /// This method wraps the view inside a GroupBox /// and adds accessibilityIdentifier to it func groupBoxAccessibilityIdentifier(_ identifier: String) -> some View { GroupBox { self } .groupBoxStyle(.contain) .accessibilityIdentifier(identifier) } } Usage: struct TestView: View { var body: some View { HStack { Image(systemName: "person") TextField("Name", text: .constant("Name")) } .padding() .onTapGesture { print("Stack Tap") } .groupBoxAccessibilityIdentifier("NAME_TEXTFIELD") } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’22