简体   繁体   English

swift-ios- Accessibility - 首先将 Voiceover 阅读/焦点顺序更改为内容视图然后导航项目?

[英]swift-ios- Accessibility - change Voiceover read/focus order to content view first then the navigation items?

I am designing an app with accessibility enabled.我正在设计一个启用了辅助功能的应用程序。 I have a rightBarButtonItem on the navigationBar , several labels in between and a button located at the bottom.我在navigationBar上有一个rightBarButtonItem ,中间有几个标签,底部有一个按钮。

I want to achieve the following behaviour: Every time the current view is loaded, the VoiceOver focus will:我想实现以下行为:每次加载当前视图时,VoiceOver 焦点将:

  1. start from the topmost label从最顶层开始 label
  2. visit all labels from top to bottom从上到下访问所有标签
  3. then, the bottom button然后,底部按钮
  4. finally, rightBarButtonItem最后,右键BarButtonItem

To ensure such order, I coded in the viewController为了确保这样的顺序,我在viewController中进行了编码

navigationController?.view.accessibilityElements = [label1, label2, button, navigationItem.rightBarButtonItem]

It did ensure the read order but the focus order failed at the rightBarButtonItem.它确实确保了读取顺序,但焦点顺序在 rightBarButtonItem 处失败。 It couldn't be clicked.无法点击。

How to solve this problem?如何解决这个问题呢?

Every time your current view is loaded, use the UIAccessibilityPostNotification method to get your purpose. 每次加载当前视图时,请使用UIAccessibilityPostNotification方法来达到目的。

There are several types of change notifications but UIAccessibilityScreenChangedNotification may be the one you're interested in. 更改通知有多种类型,但是UIAccessibilityScreenChangedNotification可能是您感兴趣的一种。

It notifies that the whole page has changed including nil or a UIObject as incoming parameters : 它通知整个页面已更改,包括nilUIObject作为传入参数:

  • With nil , the first accessible element in the page is focused. 使用nil ,页面中的第一个可访问元素将获得焦点。
  • With a UIObject , focus is shifted to the specified element . 使用UIObject焦点将转移到指定的元素

This notification comes along with a vocalization including a sound like announcing a new page. 该通知伴随着发声,包括宣布新页面的声音。


EDIT 编辑

I misunderstood your problem that is pretty much complicated than I thought. 我误解了您的问题,这个问题比我想象的要复杂得多。

If you want to have an impact on the navigation bar items, you should make the native ones inaccessible and create your own custom elements to order and/or add custom actions for instance. 如果要影响导航栏项目,则应使本机项目不可访问,并创建自己的自定义元素以订购和/或添加自定义动作。

Let's take your example with a label, a button and a navigation bar including a right bar button (I don't take into account a potential back button ). 让我们以带有标签,按钮和导航栏的示例为例,该导航栏包括一个右栏按钮(我不考虑潜在的后退按钮 )。

Follow the steps hereunder in your view controller : 在您的视图控制器中执行以下步骤:

STEP 1 : create your outlets. 步骤1 :建立您的店铺。

@IBOutlet weak var myLabel: UILabel!
@IBOutlet weak var myButton: UIButton!
@IBOutlet weak var myRightBarItem: UIBarButtonItem!

STEP 2 : hide your navigation bar items so as not to listen to their vocalization. 第2步 :隐藏导航栏项目,以免听其发声。

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    self.navigationController!.navigationBar.accessibilityElementsHidden = true
}

STEP 3 : create an accessible element for the right bar button. 步骤3 :为右栏按钮创建一个可访问元素。

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let rightButtonView = myRightBarItem.value(forKey: "view") as? UIView
    let navigationBarView = rightButtonView?.superview?.superview?.superview

    let newElt = UIAccessibilityElement(accessibilityContainer: self.view)
    newElt.accessibilityFrameInContainerSpace = navigationBarView!.convert((rightButtonView?.superview!.frame)!,
                                                                            to:self.view)
    newElt.accessibilityLabel = "new navigation right bar button"
    newElt.accessibilityTraits = .button

    //Order your elements as you wish.
    self.view.accessibilityElements = [myLabel,
                                       myButton,
                                       newElt]
}

The last step consists in adding a single action or more to your right bar button according to your application purpose. 最后一步是根据您的应用目的,在右侧栏按钮中添加一个或多个操作。

Once done, you can customize with this approach and read/focus order to content view first then the navigation items as stated in your question. 完成后,您可以使用此方法进行自定义,并先按顺序阅读/关注内容视图,然后再按问题中所述导航项。

This solution may be optimized but it allows to reach your goal. 可以优化此解决方案,但可以实现您的目标。

You can add a function as an UIAccessibility extension to set the focus on any item when you load the screen, then execute your accessibilityElements您可以添加 function 作为 UIAccessibility 扩展,以便在加载屏幕时将焦点设置在任何项目上,然后执行 accessibilityElements

extension UIAccessibility {

static func setFocusTo(_ object: Any?) {
    if UIAccessibility.isVoiceOverRunning {
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.8) {
            UIAccessibility.post(notification: .layoutChanged, argument: object)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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