Here's the sample code (using Introspect package):
import SwiftUI
import Introspect
struct ContentView: View {
let tag = 123
let id = "abc"
var body: some View {
ScrollView {
Text("Hello, world!")
.tag(tag)
.accessibility(identifier: id)
}.introspectScrollView { (scrollView) in
/// - Note: find with tag
if scrollView.viewWithTag(tag) != nil {
print("OK")
} else {
print("NOT OK")
}
/// - Note: find with accessibilityIdentifier
if findViewWithID(id, in: scrollView) != nil {
print("OK")
} else {
print("NOT OK")
}
}
}
func findViewWithID(_ id: String, in uiView: UIView) -> UIView? {
if uiView.accessibilityIdentifier == id {
return uiView
}
for view in uiView.subviews {
if let view = findViewWithID(id, in: view) {
return view
}
}
return nil
}
}
Why is tag or accessibilityIdentifier not propagated properly to the underlying view hierarchy?