Our project include UIKIt and SwiftUI, and in some case, we need to traverse all subviews, for UIKit implement views, below code:
func findView(byIdentifier identifier: String) -> UIView? {
if self.accessibilityIdentifier == identifier {
return self
}
for subview in subviews {
if let found = subview.findView(byIdentifier: identifier) {
return found
}
}
return nil
}
works well, but for SwiftUI implement views, like below code:
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
.accessibilityIdentifier("Image")
Text("Hello, world!")
.accessibilityIdentifier("Text")
}
.padding()
}
}
it can not find subviews in the ContentView, and only a view with type:
_UIHostingView<ModifiedContent<AnyView, RootModifier>>
can be found, the Image and Text is not found; And because we have set a accessibilityIdentifier property, so we also try use:
@MainActor
var accessibilityElements: [Any]? { get set }
to find sub node, but this accessibilityElements is not stable, we can find the Image and Text node in iOS26.1 system:
[AX] level=3 AccessibilityNode @ 0x000000010280fb10 id=Image
[AX] level=3 AccessibilityNode @ 0x000000010161fbf0 id=Text
but can not find it in iOS26.0 and below system. Any suggestion in how to find SwiftUI subviews? thank you