Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the accessibility label for a particular segment of a UISegmentedControl?

Tags:

We use KIF for our functional testing, and it uses the accessibility label of elements to determine where to send events. I'm currently trying to test the behaviour of a UISegmentedControl, but in order to do so I need to set different accessibility labels for the different segments of the control. How do I set the accessibility label for a particular segment?

like image 741
Simon Avatar asked Jan 12 '12 11:01

Simon


2 Answers

As Vertex said,

obj-c

[[[self.segmentOutlet subviews] objectAtIndex:3] setAccessibilityLabel:@"GENERAL_SEGMENT"]; 

swift

self.segmentOutlet.subviews[3].accessibilityLabel = "GENERAL_SEGMENT" 

some advice so you don't go crazy like I did:

  1. To scroll in accessibility mode swipe three fingers
  2. The indexes of the segments are backwards than you would expect, i.e. the furthest segment to the right is the 0th index and the furthest to the left is the n'th index where n is the number of elements in the UISegmentControl
like image 150
anders Avatar answered Nov 09 '22 03:11

anders


I'm just getting started with KIF myself, so I haven't tested this, but it may be worth a try. I'm sure I'll have the same issue soon, so I'd be interested to hear if it works.

First, UIAccessibility Protocol Reference has a note under accessibilityLabel that says:

"If you supply UIImage objects to display in a UISegmentedControl, you can set this property on each image to ensure that the segments are properly accessible."

So, I'm wondering if you could set the accessibilityLabel on each NSString object as well and be able to use that to access each segment with KIF. As a start, you could try creating a couple of strings, setting their accessibility labels, and using [[UISegmentedControl alloc] initWithItems:myStringArray]; to populate it.

Please update us on your progress. I'd like to hear how this goes

like image 23
jmac Avatar answered Nov 09 '22 05:11

jmac