问题
I encountered a strange behaviour in UI-tests. When I add a custom view into UITableViewCell, some of it's subviews are not accessible. However, it's OK in Xcode's Debug View Hierarchy.
Yes, I know about UIAccessibilityContainer but it's not a solution cause of: 1. there are regular subviews in my custom view (views, labels, images), not a drawn content. 2. I can't disable isAccessibilityElement for containerView and enable it for ImageView cause of I need both in the same time.
So, when I add my view to TableViewCell as-is, I can access only few elements from UI-tests:
Table, 0x604000196da0, traits: 35192962023424, {{5.0, 108.0}, {404.0, 586.0}}
Cell, 0x604000384b90, traits: 8589934592, {{5.0, 132.0}, {365.0, 329.0}}
StaticText, 0x6040003849f0, traits: 8589934656, {{9.0, 135.7}, {25.0, 12.7}}, identifier: 'myLabel', label: '123'
StaticText, 0x604000384780, traits: 8589934656, {{73.0, 154.3}, {293.0, 18.7}}, identifier: 'titleLabel', label: 'Title'
If I enable isAccessibilityElement for images, I can see it as well:
Table, 0x604000196da0, traits: 35192962023424, {{5.0, 108.0}, {404.0, 586.0}}
Cell, 0x604000384b90, traits: 8589934592, {{5.0, 132.0}, {365.0, 329.0}}
Image, 0x604000384ac0, traits: 8589934596, {{9.0, 235.7}, {357.0, 174.3}}, identifier: 'myImage'
StaticText, 0x6040003849f0, traits: 8589934656, {{9.0, 135.7}, {25.0, 12.7}}, identifier: 'myLabel', label: '123'
StaticText, 0x604000384780, traits: 8589934656, {{73.0, 154.3}, {293.0, 18.7}}, identifier: 'titleLabel', label: 'Title'
Image, 0x604000384370, traits: 8589934596, {{9.0, 156.3}, {56.0, 56.0}}, identifier: 'myIcon'
But, in both cases a hierarchy within Cell is plain and there are no container views.
回答1:
The solution is: use UICollectionView instead of UITableView.
Here is a correct hierarchy (isAccessibilityElement has default value for each element).
Other, 0x600000193590, traits: 8589934592, {{5.0, 132.0}, {365.0, 604.0}}
CollectionView, 0x6000001930b0, traits: 35192962023424, {{5.0, 132.0}, {365.0, 573.0}}
Cell, 0x600000192fe0, traits: 8589934592, {{5.0, 132.0}, {365.0, 329.0}}
Other, 0x600000192f10, traits: 8589934592, {{5.0, 132.0}, {365.0, 329.0}}
Other, 0x6040001900c0, traits: 8589934592, {{0.0, 129.7}, {375.0, 333.7}}, identifier: 'myView'
Other, 0x604000192e40, traits: 8589934592, {{0.0, 129.7}, {375.0, 333.9}}, identifier: 'contentView'
StaticText, 0x60400018f970, traits: 8589934656, {{4.0, 133.7}, {25.0, 12.7}}, identifier: 'myLabel', label: '123'
Image, 0x60400018fd80, traits: 8589934596, {{4.0, 154.3}, {56.0, 56.0}}, identifier: 'myIcon'
StaticText, 0x60400018f7d0, traits: 8589934656, {{68.0, 152.3}, {303.0, 18.7}}, identifier: 'titleLabel', label: 'Title'
Other, 0x60400018ee10, traits: 8589934592, {{4.0, 233.7}, {367.0, 179.2}}, identifier: 'containerView'
Image, 0x60400018eba0, traits: 8589934596, {{4.0, 233.7}, {367.0, 179.2}}, identifier: 'myImage'
来源:https://stackoverflow.com/questions/51981332/xcuielement-hierarchy-within-uitableviewcell