辅助功能焦点顺序未按预期工作[iOS]
概述 我无法获得正确的焦点顺序(iOS中的可访问性)。becomeFirstResponder()似乎覆盖了我在数组中指定的焦点顺序,并导致语音辅助功能首先读取错误的辅助功能标签 详细信息: 我有一个带有containerView的视图控制器。在里面,我可以看到我的进度条图像和文本输入字段(占位符)。这两个元素都具有isAccessibilityElement=true属性,并且它们已添加到我的焦点顺序数组中。但是,在屏幕启动时,焦点顺序转到输入字段,而不是进度条视图 在扩展测试之后,我注意到如果我删除下面的代码行,这个问题就不再是可复制的辅助功能焦点顺序未按预期工作[iOS],ios,swift,accessibility,voiceover,becomefirstresponder,Ios,Swift,Accessibility,Voiceover,Becomefirstresponder,概述 我无法获得正确的焦点顺序(iOS中的可访问性)。becomeFirstResponder()似乎覆盖了我在数组中指定的焦点顺序,并导致语音辅助功能首先读取错误的辅助功能标签 详细信息: 我有一个带有containerView的视图控制器。在里面,我可以看到我的进度条图像和文本输入字段(占位符)。这两个元素都具有isAccessibilityElement=true属性,并且它们已添加到我的焦点顺序数组中。但是,在屏幕启动时,焦点顺序转到输入字段,而不是进度条视图 在扩展测试之后,我注意到如
otpNumberTextField.becomeFirstResponder()
但这不是一个解决方案。我需要光标在文本字段,但语音功能,阅读我的进度条可访问性标签第一。如何修复它
特定场景
我注意到,只有当我的VC最后一个活动焦点是文本字段,然后转换到下一个VC(带有文本字段和进度条)时,才会出现这个问题
当我的VC最后一个活动焦点在按钮上,然后转换到下一个VC(带有一个文本字段和一个进度条)时,这个错误是不可复制的
代码片段
import UIKit
class MyViewController: UIViewController, UITextFieldDelegate {
var otpNumberTextField = UITextField()
var progressMainDot = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
setupView()
setupBinding()
}
override func viewWillAppear(_ animated: Bool) {
setupView()
textFieldDidChange(otpNumberTextField)
}
func setupView(){
let containerView = UIView()
containerView.backgroundColor = UIColor.init(named: ColourUtility.BackgroundColour)
view.addSubview(containerView)
containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
containerView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
containerView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
containerView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
//Progress Bar
let progressBarView = UIView()
containerView.addSubview(progressBarView)
progressBarView.isAccessibilityElement = true
progressBarView.accessibilityLabel = "my accessibility label"
progressBarView.translatesAutoresizingMaskIntoConstraints = false
progressMainDot.image = UIImage(named:ImageUtility.progressMain)
progressMainDot.contentMode = .scaleAspectFit
progressBarView.addSubview(progressMainDot)
//Text Field
otpNumberTextField.borderStyle = UITextField.BorderStyle.none
otpNumberTextField.font = UIFontMetrics.default.scaledFont(for: FontUtility.inputLargeTextFieldStyle)
otpNumberTextField.adjustsFontForContentSizeCategory = true
otpNumberTextField.isAccessibilityElement = true
otpNumberTextField.accessibilityLabel = AccessibilityUtility.enterVerificationCode
otpNumberTextField.placeholder = StringUtility.otpPlaceholder
otpNumberTextField.textColor = UIColor.init(named: ColourUtility.TextfieldColour)
otpNumberTextField.textAlignment = .center
otpNumberTextField.keyboardType = .numberPad
otpNumberTextField.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: .editingChanged)
containerView.addSubview(otpNumberTextField)
otpNumberTextField.becomeFirstResponder()
//Accessibility - focus order
view.accessibilityElements = [progressBarView, otpNumberTextField]
}
//... more code goes here ...
}
如果您已经设置了
accessibilityElements
,则画外音应遵守该顺序,但调用becomeFirstResponder()
会将焦点更改为该文本字段
您可以尝试下面的代码,该代码通知画外音由于布局更改而将焦点转移到新元素
UIAccessibility.post(notification: .layoutChanged, argument: progressBarView)
因此,现在您修改的方法应如下所示:
func setupView(){
.....
otpNumberTextField.becomeFirstResponder()
//Accessibility - focus order
view.accessibilityElements = [progressBarView, otpNumberTextField]
UIAccessibility.post(notification: .layoutChanged, argument: progressBarView)
.....
}
如果您已经设置了
accessibilityElements
,则画外音应遵守该顺序,但调用becomeFirstResponder()
会将焦点更改为该文本字段
您可以尝试下面的代码,该代码通知画外音由于布局更改而将焦点转移到新元素
UIAccessibility.post(notification: .layoutChanged, argument: progressBarView)
因此,现在您修改的方法应如下所示:
func setupView(){
.....
otpNumberTextField.becomeFirstResponder()
//Accessibility - focus order
view.accessibilityElements = [progressBarView, otpNumberTextField]
UIAccessibility.post(notification: .layoutChanged, argument: progressBarView)
.....
}
Om Prakash我以前试过,但解决方案似乎有问题。当我使用下面的代码时,VoiceOver仍然首先开始读取InputExtField,并且只在一段时间后切换并开始读取我的进度条。我目前有一个解决方案,通过语音“otpNumberTextField.AccessibilityYelementsHidden=true”隐藏文本字段,然后在5秒后使用函数将其取消隐藏。这样,画外音首先开始阅读进度条。还请注意,只有当以前的VC集中在另一个输入字段时,才会出现此错误(上次活动的焦点在按钮上的VC不会出现此错误)。Om Prakash我以前尝试过此方法,但解决方案似乎有问题。当我使用下面的代码时,VoiceOver仍然首先开始读取InputExtField,并且只在一段时间后切换并开始读取我的进度条。我目前有一个解决方案,通过语音“otpNumberTextField.AccessibilityYelementsHidden=true”隐藏文本字段,然后在5秒后使用函数将其取消隐藏。这样,画外音首先开始阅读进度条。还请注意,只有当以前的VC集中在另一个输入字段上时,才会出现此错误(对于上次活动的焦点在按钮上的VC不会出现此错误)。