chickenparm
chickenparm

Reputation: 1630

UIAccessibilityElement with button trait adding "one of one" after voice over speaks button

I am using UIAccessibilityElement to encapsulate information from multiple labels and buttons in a custom UITableViewCell. When Voice Over is enabled and a user swipes through each element I want specific elements to be grouped together. For example, instead of speaking "Favorite Food" then the user has to swipe again to hear "Pizza" it will speak "Favorite Food Pizza."

In my subclassed cell I have this working by overriding accessibilityElements. However, I am having an issue when I add a UIButton to accessibilityElements. In the code below I create viewProfileButtonAccessibilityElement. I add the accessibility label and the trait .button. When spoken by by Voice Over it says "View Profile Picture button one of one." I can't figure out where "one of one" is coming from. Any idea what is causing Voice Over to add "one of one" after speaking the word button?

  private var cachedAccessiblityElements: [Any]?

  override var accessibilityElements: [Any]? {
    set {
      cachedAccessiblityElements = newValue
    }
    get {
      // Return the acessibility elements if we've already created them
      if let cachedAccessiblityElements = cachedAccessiblityElements {
        return cachedAccessiblityElements
      }

      var elements = [UIAccessibilityElement]()

      if let favoriteFoodTitle = favoriteFoodTitle.text, let favoriteFood = favoriteFood.text {
        let favoriteFoodAccessibilityElement = UIAccessibilityElement(accessibilityContainer: self)
        favoriteFoodAccessibilityElement.accessibilityLabel = favoriteFoodTitle + " " + favoriteFood
        favoriteFoodAccessibilityElement.accessibilityFrameInContainerSpace = self.favoriteFoodTitle.frame.union(self.favoriteFood.frame)
        elements.append(favoriteFoodAccessibilityElement)
      }

      let viewProfileButtonAccessibilityElement = UIAccessibilityElement(accessibilityContainer: self)
      viewProfileButtonAccessibilityElement.accessibilityLabel = "View Profile Picture"
      viewProfileButtonAccessibilityElement.accessibilityTraits = UIAccessibilityTraits.button
      viewProfileButtonAccessibilityElement.accessibilityFrameInContainerSpace = self.viewProfileButton.frame
      elements.append(viewProfileButtonAccessibilityElement)

      cachedAccessiblityElements = elements
      return cachedAccessiblityElements
    }
  }

Upvotes: 3

Views: 1694

Answers (1)

XLE_22
XLE_22

Reputation: 5671

I can't figure out where "one of one" is coming from. Any idea what is causing Voice Over to add "one of one" after speaking the word button?

Every UIControl button you're creating in a cell with the .button trait will be vocalized the way you mentioned.

Whatever the number of created buttons in a tableviewcell, all will be vocalized with the same suffix indicating the cell they belong to and the total number of cells in the section.

"one of one" in your example means that your button is in the first cell and that you have only one cell in your section.

For instance, if you create two buttons in the third cell of a section containing ten cells, you will hear the suffix "three of ten" for your two buttons.

I hope this explanation is clear enough to understand where your "one of one" is coming from as desired.

Upvotes: 2

Related Questions