简体   繁体   中英

How to set focus on second Accessibility element?

I've tried to set the focus to the second element on an alert view:

 UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, secondElement);

It works on iOS 12 and before, but after iOS 13, the focus will initially be on the second element, but will soon be on the first.

Sometimes there's a bit of a race between your Post Notification and one posted by the system when a new view appears. You may need to delay your notification, which is a bit of a hack, but it usually works.

I think you want to post a UIAccessibilityLayoutChangedNotification if you're opening an alert. There is relatively little difference between the screenChanged and layoutChanged notification types – they both cause VoiceOver to refresh its cache of what its interacting with on the screen – but there is a difference in their default behaviour.

A screenChanged notification should fire when an entirely new screen appears, and it will cause focus to move to the first focusable element on the new screen.

A layoutChanged notification should fire when a part of the current screen changes, such as toggling an expand-collapse panel, but it does not move focus to an element by default. If you wish, you provide the element to receive focus as the second argument of the layoutChanged notification.

In Swift, this might look something like:

if UIAccessibility.isVoiceOverRunning {
    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
        UIAccessibility.post(notification: .layoutChanged, argument: secondElement)
    }
}

In Objective C:

if (UIAccessibilityIsVoiceOverRunning()) {
    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.0) {
        UIAccessibilityPostNotification(
            UIAccessibilityLayoutChangedNotification,
            secondElement);
    }
}

Important note: Be careful changing focus in this way. While opening an alert, it may be acceptable to change focus in this way, but you must test to make sure that the content of the Alert is actually announced by VoiceOver when opening it while using this technique. It may be that VoiceOver users miss the message. Generally, setting an unexpected focus position can cause more problems than it's worth for people who rely on such software.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM