简体   繁体   English

辅助功能将焦点设置为导航栏标题项

[英]Accessibility set focus to navigation bar title item

Overview:概述:

I would like to set the accessibility focus to the navigation bar's title item.我想将可访问性焦点设置为导航栏的标题项。

By default the focus is set from top left, meaning the back button would be on focus.默认情况下,焦点是从左上角设置的,这意味着后退按钮将处于焦点上。

I would like the title item to be in focus.我希望标题项目成为焦点。

Attempts made so far:迄今为止的尝试:

    UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification,
                                    navigationController?.navigationBar.items?.last)

Problem:问题:

  • The above code makes no difference, the back button is still in focus.上面的代码没有区别,后退按钮仍然是焦点。

Possible Cause:可能的原因:

  • Not able to get the item corresponding to the title to be able to set the focus.无法获取到title对应的item才能设置焦点。

Solution 1 I don't like it, but it was the minimum amount of hacking that does not rely on digging through hidden subviews (internal implementation of UINavigationBar view hierarchy).解决方案 1我不喜欢它,但它是不依赖于挖掘隐藏子视图(UINavigationBar 视图层次结构的内部实现)的最小数量的黑客攻击。

First in viewWillAppear, I store a backup reference of the back button item, and then remove the back button item (leftBarButtonItem):首先在viewWillAppear中,我存储了后退按钮项的备份引用,然后移除后退按钮项(leftBarButtonItem):

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

    backButtonBackup = self.navigationItem.leftBarButtonItem
    self.navigationItem.leftBarButtonItem = nil  
}

Then I restore the back item, but only after I dispatch the screen changed event in viewDidAppear() :然后我恢复了后面的项目,但只有在我在 viewDidAppear() 中发送屏幕更改事件之后:

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

    UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, nil)
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [weak self] in
        self?.navigationItem.leftBarButtonItem = self?.backButtonBackup
    }
}

Solution 2: Disable all accessibility on the nav bar and view controller up until viewDidAppear() is finished:解决方案 2:禁用导航栏和视图控制器上的所有辅助功能,直到 viewDidAppear() 完成:

    self.navigationController.navigationBar.accessibilityElementsHidden = true
    self.view.accessibilityElementsHidden = true

, and then in viewDidAppear manually dispatching the layout element accessibility focused event to the label subview of UINavigationBar: ,然后在 viewDidAppear 中手动调度布局元素无障碍聚焦事件到 UINavigationBar 的标签子视图:

UIAccessibilityPostNotification( UIAccessibilityLayoutChangedNotification, self.navigationController.navigationBar.subviews[2].subviews[1])   
//  The label buried inside the nav bar.   Not tested on all iOS versions.  
// Alternately you can go digging for the label by checking class types. 
// Then use DispatchAsync, to re-enable accessibility on the view and nav bar again... 

I'm not a fan of this method either.我也不喜欢这种方法。

DispatchAsync delay in viewDidAppear seems to be needed in any case - and I think both solutions are still horrible.在任何情况下似乎都需要 viewDidAppear 中的 DispatchAsync 延迟 - 我认为这两种解决方案仍然很糟糕。

First, we'll need to create an useful extension:首先,我们需要创建一个有用的扩展:

extension UIViewController {
    func setAccessibilityFocus(in view: UIView) {
        UIAccessibility.post(notification: .screenChanged, argument: view)
    }
}

Then, we'll be able to set our focus in the navigation bar title like this:然后,我们将能够像这样在导航栏标题中设置焦点:

setAccessibilityFocus(in: self.navigationController!.navigationBar.subviews[2].subviews[1])

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

相关问题 辅助功能:如何始终将焦点设置在导航项的标题视图上 - Accessibility: How to always set the focus on the navigation item's title view 设置导航栏标题 - Set title of navigation bar 单击iOS中的选项卡项时,无法在导航栏中设置标题 - Can not set title in Navigation bar when click on tabbar item in iOS 设置父导航栏的标题 - Set title of parent navigation bar 设置导航栏标题的对齐方式 - set the alignment of title of navigation bar 如何在代码中将辅助功能标识符添加到导航栏标题 - How to add Accessibility identifier to navigation bar title in code 在启动时以编程方式为5个选项卡栏项目设置选项卡栏标题,其中4个选项卡嵌入在Navigation Controller中,而1个则没有。 目标C - Set Tab Bar title programatically at Startup for 5 Tab Bar item, 4 of which is embedded in Navigation Controller, 1 is not. Objective C 我不能同时在情节提要中将标题和图像同时设置到导航栏上的右侧栏项目吗? - Cann't I set title and Image to the right bar item on navigation bar at the same time in storyboard? 更改导航栏按钮项的标题 - Change title of a navigation bar button item TableViewController导致TabBar项目标题更改为导航栏标题 - TableViewController is causing TabBar item title to change to navigation bar title
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM