以下是示例代码(使用Introspect包):
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
}
}
为什么tag
或accessibilityIdentifier
无法正确传播到基础视图层次结构?
答案 0 :(得分:2)
无论如何,解决方法非常简单:
struct TagView: UIViewRepresentable {
let tag: Int
func makeUIView(context: Context) -> UIView {
let uiView = UIView()
uiView.tag = tag
return uiView
}
func updateUIView(_ uiView: UIView, context: Context) {
uiView.tag = tag
}
}
viewWithTag(_:)
在视图层次结构中找到它:ZStack {
TagView(tag: 123)
Text("Hello, world!")
}