How do I set the accessibility label for a particu

2019-01-25 04:50发布

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?

10条回答
ら.Afraid
2楼-- · 2019-01-25 04:56

You guys want to see how Apple recommends it be done?

It's FUGLY.

This is from this example:

func configureCustomSegmentsSegmentedControl() {
    let imageToAccessibilityLabelMappings = [
        "checkmark_icon": NSLocalizedString("Done", comment: ""),
        "search_icon": NSLocalizedString("Search", comment: ""),
        "tools_icon": NSLocalizedString("Settings", comment: "")
    ]

    // Guarantee that the segments show up in the same order.
    var sortedSegmentImageNames = Array(imageToAccessibilityLabelMappings.keys)
    sortedSegmentImageNames.sort { lhs, rhs in
        return lhs.localizedStandardCompare(rhs) == ComparisonResult.orderedAscending
    }

    for (idx, segmentImageName) in sortedSegmentImageNames.enumerated() {
        let image = UIImage(named: segmentImageName)!

        image.accessibilityLabel = imageToAccessibilityLabelMappings[segmentImageName]

        customSegmentsSegmentedControl.setImage(image, forSegmentAt: idx)
    }

    customSegmentsSegmentedControl.selectedSegmentIndex = 0

    customSegmentsSegmentedControl.addTarget(self,
                                             action: #selector(SegmentedControlViewController.selectedSegmentDidChange(_:)),
                                             for: .valueChanged)
}

They apply the accessibility labels to images, and then attach the images. Not too different from the above answer.

查看更多
劳资没心,怎么记你
3楼-- · 2019-01-25 05:05

This is an old question but just in case anyone else runs up against this I found that the segments automatically had an accessibility label specified as their text. So if two options were added of Option 1 and Option 2. A call to

[tester tapViewWithAccessibilityLabel:@"Option 2"];

successfully selected the segment.

查看更多
Ridiculous、
4楼-- · 2019-01-25 05:09

You can't rely on the index in the subviewsarray for the position. For customisation of the individual subviews I sort the subviews on their X Position before setting any propery.What would also be valid for accesibilityLbel.

let sortedViews = self.subviews.sorted( by: { $0.frame.origin.x < $1.frame.origin.x } )

sortedViews[0].accessibilityLabel = "segment_full"
sortedViews[1].accessibilityLabel = "segment_not_full"
查看更多
三岁会撩人
5楼-- · 2019-01-25 05:11

Each segment of UISegmentedControl is UISegment class instance which subclass from UIImageView. You can access those instances by subviews property of UISegmentedControl and try to add accessibility for them programmatically.

查看更多
Anthone
6楼-- · 2019-01-25 05:12

This is an old question but just in case anyone else runs up against this I found that the segments automatically had an accessibility label specified as their text.

Further to Stuart's answer, I found it really useful when writing test cases to turn on 'Accessibility Inspector' on the Simulator (Settings -> General -> Accessibility -> Accessibility Inspector). You'd be surprised how many elements already have accessibility labels included, like in the standard iOS UI elements or even third party frameworks.

Facebook log in button with accessibility label

Note: Gestures will now be different - Tap to view accessibility information, double tap to select. Minimizing the Accessibility Inspector window (by tapping the X button) will return the gestures back to normal.

查看更多
三岁会撩人
7楼-- · 2019-01-25 05:13

another option if not willing to set accesibility label might be calculating the poistion of each segment part and use

[tester tapScreenAtPoint:segementPosition];

to trigger the actions

查看更多
登录 后发表回答