Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Accessibility, how to make focus go back to the last item after dismiss the alert

I have a button and when I tap the button, there will be an alert.

When I have voiceover on and tap the button, then tap the alert OK button to dismiss the alert, the focus will go back to the top of the page/back button, rather than the button that triggers the alert.

I'm using this chunk of code to move the focus back to the button:

let dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0 * Double(NSEC_PER_SEC)))

dispatch_after(dispatchTime, dispatch_get_main_queue(), {
  UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, button)
})

The problem is after you dismiss the alert, the focus will go back to the top and read the label first, then go back to the button. The focus is not going to the button directly, which looks pretty lame.

I checked some Apple app, and I find that after they dismiss the alert, the focus will go back to the previous element before the alert pops up. Any idea how to achieve this? Thanks!

like image 552
Ke MA Avatar asked Apr 28 '16 20:04

Ke MA


1 Answers

I was having this issue earlier today - the way I solved it is by posting a UIAccessibilityScreenChangedNotification instead of a UIAccessibilityLayoutChangedNotification:

UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, button)

Layout changes are for a change in layout in the current context of your view. Screen changes are for use when a change in context is experienced by the user, i.e. dismissing an alert and returning back to the presenting view controller.

You can get rid of the dispatch_after block, too - using the screen changed notification instead will give you the same functionality you describe from one of Apple's apps.

Here is a link to an answer explaining the difference between UIAccessibilityLayoutChangedNotification and UIAccessibilityScreenChangedNotification.

like image 129
John Rogers Avatar answered Oct 22 '22 03:10

John Rogers