簡體   English   中英

swift-ios- Accessibility - 首先將 Voiceover 閱讀/焦點順序更改為內容視圖然后導航項目?

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

我正在設計一個啟用了輔助功能的應用程序。 我在navigationBar上有一個rightBarButtonItem ,中間有幾個標簽,底部有一個按鈕。

我想實現以下行為:每次加載當前視圖時,VoiceOver 焦點將:

  1. 從最頂層開始 label
  2. 從上到下訪問所有標簽
  3. 然后,底部按鈕
  4. 最后,右鍵BarButtonItem

為了確保這樣的順序,我在viewController中進行了編碼

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

它確實確保了讀取順序,但焦點順序在 rightBarButtonItem 處失敗。 無法點擊。

如何解決這個問題呢?

每次加載當前視圖時,請使用UIAccessibilityPostNotification方法來達到目的。

更改通知有多種類型,但是UIAccessibilityScreenChangedNotification可能是您感興趣的一種。

它通知整個頁面已更改,包括nilUIObject作為傳入參數:

  • 使用nil ,頁面中的第一個可訪問元素將獲得焦點。
  • 使用UIObject焦點將轉移到指定的元素

該通知伴隨着發聲,包括宣布新頁面的聲音。


編輯

我誤解了您的問題,這個問題比我想象的要復雜得多。

如果要影響導航欄項目,則應使本機項目不可訪問,並創建自己的自定義元素以訂購和/或添加自定義動作。

讓我們以帶有標簽,按鈕和導航欄的示例為例,該導航欄包括一個右欄按鈕(我不考慮潛在的后退按鈕 )。

在您的視圖控制器中執行以下步驟:

步驟1 :建立您的店鋪。

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

第2步 :隱藏導航欄項目,以免聽其發聲。

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

    self.navigationController!.navigationBar.accessibilityElementsHidden = true
}

步驟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]
}

最后一步是根據您的應用目的,在右側欄按鈕中添加一個或多個操作。

完成后,您可以使用此方法進行自定義,並先按順序閱讀/關注內容視圖,然后再按問題中所述導航項。

可以優化此解決方案,但可以實現您的目標。

您可以添加 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