Accessibility for iOS, VoiceOver read order issue

2019-01-15 02:18发布

问题:

Is it possible to change the order in which the VoiceOver feature for accessibility in iPad reads out the elements, when the 'Two-finger Flick Down' gesture is done?

For the attached image, which contains 3 labels and a button, the VoiceOver reads the elements in the following way,

Label 1 -> Label 2 -> Button -> Label 3

Can the order be changed to,

Label 1 -> Label 2 -> Label 3 -> Button

回答1:

The quickest way to achieve this for your example is to place the three labels in a transparent UIView subclass to serve as a container for your labels. This subclass will have to be properly setup to let VoiceOver know how to interpret it. If your deployment target is iOS6 then you can simply answer the "should group accessibility children" question in this subclass.

-(BOOL)shouldGroupAccessibilityChildren{
    return YES;
}

For below iOS6 it would be more complicated, except that your UIView container subclass would contain only UILabels which are accessibility elements. You could implement it like this:

-(BOOL)isAccessibilityElement{
    return NO;
}
-(NSInteger)accessibilityElementCount{
    return self.subviews.count;
}
-(id)accessibilityElementAtIndex:(NSInteger)index{
    return [self.subviews objectAtIndex:index];
}
-(NSInteger)indexOfAccessibilityElement:(id)element{
    return [self.subviews indexOfObject:element];
}

I have tested this example code and it does what you are looking for, if you need any clarification please add a comment. Always happy to help make things more accessible.



回答2:

I tried setting the shouldGroupAccessibilityChildren to YES but it didn't work for me.

What did work for me was setting the accessibility label of the parent view directly (because I wanted all the items to be read in one go/one VoiceOver gesture).

[cell setAccessibilityLabel:[NSString stringWithFormat:@"%@, %@", cityLabel, temperatureLabel]];

The above snippet of codes is from Apple's documentation Enhancing the Accessibility of Table View Cells



回答3:

I think you can do it in the storyboard. The VoiceOver order is determined by the order of the views in the document outline.

Just drag and drop the views in the view hierarchy in the right order.



回答4:

In Swift, attaching an IBOutlet to the parent UIView, then setting shouldGroupAccessibilityChildren to true will suffice.

abc.shouldGroupAccessibilityChildren = true

I did note that if also setting isAccessibilityElement = true the grouping will not take effect. Similarly, checking the accessibility checkbox in the storyboard or xib will also prevent the grouping from taking place.