UICollectionView 中的自定义子容器视图未显示在 iOS UIAutomation logElementTree 或 Xcode UI 测试 debugDescription 中

custom sub container views in UICollectionView not displayed in iOS UIAutomation logElementTree or Xcode UI Testing debugDescription

顶级容器视图是一个自定义 UICollectionView。我在充值位置添加了两个自定义容器视图。层级树如下

custom UICollectionView

|- custom UIView container => 1 ImageButton

|- custom UIView container => 8 Buttons

|- custom UICollectionCells

Voice Over 可以正确响应这些按钮。

但是当我尝试将UIAutomationtarget.logElementTree()一起使用时,元素树只找到cutom UICollectionView下的UICollectioncell列表,它找不到自定义子视图容器及其子视图。

然后我尝试 Xcode UI 测试,使用 app.debugDescription。结果仍然只包含自定义 UICollectionView 中的 UICollectionCells,没有按钮。

如何让它们出现在 UIAutomation 元素树中?

最后通过将 Aspects 与自定义集合类别一起使用:

在自定义集合类别中,覆盖 +(void)load:

  • 添加aspect块来初始化stong 属性 a,在执行initWithFrame:collectionViewLayout:initWithCoder:
  • 后存储所有accessibilityElements
  • add aspect block 添加subview到属性 a执行后addSubview:

最后,使用 属性 a

实现 UIAccessibilityContainer 协议方法

棘手的部分: 使用 objc_setAssociatedObjectobjc_getAssociatedObject 来处理 a