When you add a UISegmentedControl to a view, UIAccessibility will focus on it and say:
"(Selected) ItemName Button 1 of 2"
"ItemName Button 2 of 2"
I have a custom control that has UIButtons that toggle similar to a UISegmentedControl. But what I'm trying to figure out is how to get the Voice Over to announce the n of n at the end.
The closest thing that I've found is assigning the .accessibilityTraits = .tabBar on the container. The issue is that it announces:
"ItemName Button Tab 2 of 2"
But to conform to our accessibility guidelines we can't have it announce "tab".
https://developer.apple.com/documentation/uikit/uiaccessibility/uiaccessibilitytraits/1648592-tabbar
Short of just writing a custom accessibilityLabel is there anything within UIAccessibility that can handle this logic?
I have a custom control that has UIButtons that toggle similar to a UISegmentedControl. But what I'm trying to figure out is how to get the Voice Over to announce the n of n at the end.
UIButton elements in the accessibilityElements array of the custom control that acts like a container.UIButton, set accessibilityLabel by inserting the result of the previous research.
Here's a kind of logic that should help you reach your purpose with a little bit of code as follows for instance (Xcode 10.2.1, Swift 5.0, iOS 12):
class ButtonsViewController: UIViewController {
@IBOutlet weak var myCustomContainer: UIView!
@IBOutlet weak var btn1: UIButton!
@IBOutlet weak var btn2: UIButton!
@IBOutlet weak var btn3: UIButton!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
myCustomContainer.accessibilityElements = [btn1!, btn2!, btn3!]
let nbButtons = myCustomContainer.accessibilityElements?.count
for (index, elt) in (myCustomContainer.accessibilityElements?.enumerated())! {
let btn = elt as! UIButton
let btnName = btn.titleLabel?.text
btn.accessibilityLabel = btnName! + String(index + 1) + " of " + String(describing: nbButtons!)
}
}
}