SwiftUI Button accessibilityElement not combining children

When using .accessibilityElement(children: .contain) on a SwiftUI Button, the children do not show up in the accessibility inspector. Instead, the Button is the only element shown and its identifier is a combination of all the children. How can I make only the children identifiers show?

Button {
} label: {
      VStack {
         Image(systemName: "star")
             .accessibilityIdentifier("logo_image")
         Text("Subtitle")
             .accessibilityIdentifier("subtitle_text")
        }
}
.accessibilityElement(children: .contain)

If I remove the button and only use the VStack the elements appear individually. This is what I am trying to achieve while using the Button.

VStack {
     Image(systemName: "star")
          .accessibilityIdentifier("logo_image")
     Text("Subtitle")
           .accessibilityIdentifier("subtitle_text")
 }

Answered by in 745038022

Thanks for your feedback.

Buttons are by default combined, and we don't currently have a way to "uncombine" combined elements.

But in this case, looks like you want XCUITest to access the inner views inside Button, and I believe XCUITest won't be affected by .accessibilityElement(children:)` modifier generally. So although Accessibility Inspector shows you the combined Button element, you should still be able to test individual elements inside Buttons for XCUITest.

Accepted Answer

Thanks for your feedback.

Buttons are by default combined, and we don't currently have a way to "uncombine" combined elements.

But in this case, looks like you want XCUITest to access the inner views inside Button, and I believe XCUITest won't be affected by .accessibilityElement(children:)` modifier generally. So although Accessibility Inspector shows you the combined Button element, you should still be able to test individual elements inside Buttons for XCUITest.

SwiftUI Button accessibilityElement not combining children
 
 
Q