voice over can only see a page of a uicollectionview

你离开我真会死。 提交于 2019-11-30 02:09:33

This answer worked for me, too. Thanks!

There is one other call you must have enabled to get this to work. Otherwise your method (void)accessibilityElementDidBecomeFocused will never get called. You must enable accessibility on the object Cell.

  1. Option 1: In ViewController, set the cell instance to have accessibility.

    Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
    [cell setIsAccessibilityElement:YES];
    
  2. Option 2: Implement the accessibility interface in the cell object:

    - (BOOL)isAccessibilityElement
    {
        return YES;
    }
    
    - (NSString *)accessibilityLabel {
        return self.label.text;
    }
    
    - (UIAccessibilityTraits)accessibilityTraits {
        return UIAccessibilityTraitStaticText;  // Or some other trait that fits better
    }
    
    - (void)accessibilityElementDidBecomeFocused
    {
        UICollectionView *collectionView = (UICollectionView *)self.superview;
        [collectionView scrollToItemAtIndexPath:[collectionView indexPathForCell:self] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally|UICollectionViewScrollPositionCenteredVertically animated:NO];
        UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, self);
    }
    

After hours and hours of headache, the solution was really simple. If anyone else comes across a similar problem, this is what i did:

In the subclass of UICollectionViewCell that you are using for your CollectionView, override accessibilityElementDidBecomeFocused and implement it like this:

- (void)accessibilityElementDidBecomeFocused
{
    UICollectionView *collectionView = (UICollectionView *)self.superview;
    [collectionView scrollToItemAtIndexPath:[collectionView indexPathForCell:self] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally|UICollectionViewScrollPositionCenteredVertically animated:NO];
    UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil);
}

Stephen's answer worked for me! Thanks.

I want to add that this seems to affect only iOS6; it looks like they fixed it in iOS7.

Also, you can make the scrolling slightly faster and cleaner by passing self instead of nil to UIAccessibilityPostNotification -- like so:

- (void)accessibilityElementDidBecomeFocused {    
    UICollectionView *collectionView = (UICollectionView *)self.superview;
    [collectionView scrollToItemAtIndexPath:[collectionView indexPathForCell:self] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally|UICollectionViewScrollPositionCenteredVertically animated:NO];
    UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, self);
}

This is how you would do it in Swift:

override func accessibilityElementDidBecomeFocused() {
    guard let collectionView = superview as? UICollectionView,
        let indexPath = collectionView.indexPath(for: self) else {
        return
    }

    collectionView.scrollToItem(at: indexPath, at: .left, animated: true)
    UIAccessibility.post(notification: .layoutChanged, argument: nil)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!