【发布时间】:2021-01-29 00:24:00
【问题描述】:
这是示例代码(使用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 没有正确传播到底层视图层次结构?
【问题讨论】:
-
相反的问题:为什么应该这样做?
-
我知道从技术上讲它不是同一个视图,但我有点希望它能够工作,因为我还能在层次结构中找到特定的 SwiftUI 视图以获取它的框架并执行滚动没有
ScrollViewReader(为了也支持iOS 13)?
标签: ios swift swiftui uikit introspection