ios UISearchBar访问用于UIAccessibility的明文按钮

pvabu6sv  于 2023-04-22  发布在  iOS
关注(0)|答案(2)|浏览(83)

对于符合UIAccessibility和VoiceOver的个人应用程序,我需要更改UISearchBar中的明文按钮(灰色十字)的UIAccessibilty标签。
我不知道如何访问这个元素。
我试着

class ViewController: UIViewController, UISearchBarDelegate
{
  @IBOutlet weak var searchBar: UISearchBar!

  weak var btnCancel: UIButton?

  override func viewDidLoad()
  {
    super.viewDidLoad()

    for subView in searchBar.subviews
    {
      for subSubView in subView.subviews
      {
        if subSubView.isKindOfClass(UIButton)
        {
          btnCancel = subSubView as? UIButton

          btnCancel?.isAccessibilityElement = true

          btnCancel?.accessibilityLabel =  NSLocalizedString("Clear Text", comment: "")
        }
      }
    }
  }
}

我在viewDidLoad上试了一下;searchBar textDidChange ;searchBarTextDidBeginEditing ;
我从来没找到什么纽扣
欢迎你的帮助

xxls0lw8

xxls0lw81#

Thanks to Joanna Carter from french website Cocoacafe.fr
解决方案:

@IBOutlet weak var searchBar: UISearchBar!

  override func viewDidLoad()
  {
    super.viewDidLoad()

    for subView in searchBar.subviews
    {
      for sView in subView.subviews
      {
        if let textField = sView as? UITextField,
           let clearButton = textField.valueForKey("clearButton") as? UIButton
        {
          clearButton.isAccessibilityElement = true

          clearButton.accessibilityLabel =  NSLocalizedString("Clear Text", comment: "")
        }
      }
    }
  }
pepwfjgg

pepwfjgg2#

如果let searchTextField = self.searchBar.value(forKey:“searchField”)作为?UITextField,让clearButton = searchTextField.value(forKey:“clearButton”)as?UIButton { clearButton.isAccessibilityElement = true clearButton.tintColor = .red clearButton.accessibilityLabel = NSLocalizedString(“Clear Button accessibility labe;“,评论:“”)}

相关问题