Ke MA
Ke MA

Reputation: 771

Accessibility collectionView focus changes when reloaded, iOS

I'm working on the accessibility of a calendar which is actually a collectionView. Whenever a cell is tapped, the collectionView will be reloaded by calling

[self.collectionView reloadData];

The problem is if the voiceOver is running, the focus will move to another place after the cell tapped because that cell is reused on somewhere else.

Is there anyway to keep the focus where it was after the reloadData? Thanks!

Upvotes: 10

Views: 3896

Answers (3)

s3cretshadow
s3cretshadow

Reputation: 237

We have a collection view and this collection view reloading data in every second. When user taps cell, focus changing after every reload, so collectionview cell selects wrong cell at indexPath.

Create a protocol for delegation in cell:

protocol AccessibilityCellProtocol{

    func accessibilityFocused(cell:UICollectionViewCell)
   
}

Override accessibilityElementDidBecomeFocused in your cell:

override class func accessibilityElementDidBecomeFocused(){
    self.delegate.accessibilityFocused(cell:self)
}

In view controller create an selectedIndexPath variable. Assign it in delegation method.

func accesibilityFocused(cell:UITableViewCell){
    selectedIndex = collectionView.indexPath(for: cell)
}

And in your didSelectItemAtIndexPath method:

if UIAccessibility.isVoiceOverRunning{
    cellTappedWith(indexPath:selectedIndex)
    return
}
cellTappedWithIndexPath(indexPath:indexPath)

Upvotes: 0

Ke MA
Ke MA

Reputation: 771

Just find a workaround for this. The focus is changed because the focused cell is reused somewhere else when doing [colleciontView reloadData].

So if we reload the collectionViewCells one by one, that focused cell will not be used anywhere else. I call this method to reload the collectionView when VoiceOver is running.

- (void)reloadCalendarCollectionView {

  NSInteger items = [self.calendarItems count];

  for (NSInteger i = 0; i < items; i++) {
    [self.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:i inSection:1] ]];
  }

}

Upvotes: 6

Thurman_1776
Thurman_1776

Reputation: 21

You could try doing self.collectionView.accessibilityElementsHidden = YES before reloading data. Then, when it completes, you will have to do the inverse & then post a notification for the cell you're looking for.

Upvotes: 0

Related Questions