Search code examples
iosaccessibilityui-automationios-ui-automationxcode-ui-testing

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


The top level container view is a custom UICollectionView. I have added two custom container views on top up location. The hierarchy tree is like below

custom UICollectionView

|- custom UIView container => 1 ImageButton

|- custom UIView container => 8 Buttons

|- custom UICollectionCells

Voice Over could respond with these buttons correctly.

But when I tried to use UIAutomation with target.logElementTree(), the element tree only finds the UICollectioncell list under the cutom UICollectionView, It cannot find the custom subview container and their children.

Then I tried Xcode UI Testing, using app.debugDescription. The result still only contains UICollectionCells in the custom UICollectionView, without buttons.

How could I make them appear in the UIAutomation element tree?


Solution

  • Finally by using Aspects with the custom collection category:

    In custom collection category, override +(void)load:

    • add aspect block to init the stong property a, which storing all accessibilityElements after execution initWithFrame:collectionViewLayout: or initWithCoder:
    • add aspect block to add subview to property a after execution addSubview:

    finally, implement UIAccessibilityContainer protocol methods using the property a

    Tricky part: use objc_setAssociatedObject and objc_getAssociatedObject to handle a