Search code examples
iosswiftuicollectionviewaccessibilityvoiceover

My UICollectionView doesn't get populated with any cells when VoiceOver is on


I'm hitting a peculiar problem with accessibility in iOS 13.

I have a calendar that's implemented as a UICollectionView. If VoiceOver is turned on, the collection view is empty - it simply doesn't get populated with any UICollectionViewCells.

I've overridden the correct UICollectionViewDataSource calls. numberOfSections and collectionView(numberOfItemsInSection) are called, and returns correct nonzero values. But collectionView(cellForItemAt) is never called. That's what doesn't make any sense to me; it knows how many sections and how many items I have, but it doesn't care to ask about the items.

If I turn off VoiceOver, then it'll call collectionView(cellForItemAt) and it populates my collection view properly. And in iOS 12, everything works fine whether or not VoiceOver is on.

What am I missing, or how can I debug this? How could the state of VoiceOver affect whether my collection view gets populated?


Solution

  • We reported the problem to our Apple representative and got a response. What's happening is that in iOS 13, VoiceOver asks the collection view for information about a larger rect than is visible; this was an intentional change to support certain types of collection views whose content size is not known up-front. So it was calling our override of layoutAttributesForElements(in rect:) with a large negative Y values for the rect, and we didn't have any elements out there.

    Our fix was to ignore the Y value of the rect we were given, and instead use a Y value of 0. iOS was then able to find our collection view cells.