I'm currently working on improving accessibility in my iOS app and learning best practices along the way. While testing with VoiceOver, I noticed that when swiping through apps on the iOS Home Screen, VoiceOver announces things like: "Weather, Double-tap to open."
The "Double-tap to open" part seems to be a default behavior that users are familiar with. I would like to achieve similar behavior in my own app.
I have created a custom view that functions as a button. Due to specific design requirements, using a standard UIButton is not an option in this case. Instead, I’ve configured the custom UIView with the following properties:
customView.isAccessibilityElement = true
customView.accessibilityLabel = "Options"
customView.accessibilityHint = "Open options menu"
customView.accessibilityTraits = [.button]
customView.isUserInteractionEnabled = true
I expected that by setting the .button trait and providing appropriate context, VoiceOver would automatically append "Double-tap to open", but it does not.
My question is:
Is there a way to have VoiceOver automatically say "Double-tap to open" for a custom button, or do I need to include that instruction manually in the accessibilityHint?
Short answer: You have to include that instruction manually in the accessibilityHint.
However, if you want to have this behavior as the default on your app, you can use the following code to extend Accessibility and include "Double tap to" at every UIView that has a .button trait (or you can modify it in the code as well).
You simply need to use accessibilityCustomHint instead of accessibilityHint where you want to achieve this behavior.
extension UIAccessibilityElement {
var accessibilityCustomHint: String? {
get { return self.validateDoubleTap() }
set { self.accessibilityHint = validateDoubleTap() }
}
private func validateDoubleTap() -> String? {
guard self.accessibilityTraits.contains(.button) else { return nil }
guard let hint = accessibilityHint else { return nil }
if hint.hasPrefix("Double tap to ") {
return hint
}
return "Double tap to " + hint
}
}