0

[Q] Can UIAccessibilityTraits.tabBar only be applied to UITabBar? Is there any way to make UIViews have tabBar UIAccessibilityTraits?

Works fine with UITabBarItems in UITabBar.

// This works. No problem
let first = UITabBarItem(title: "firstTab", image: UIImage(systemName: "chart.line.uptrend.xyaxis"), selectedImage: UIImage(systemName: "chart.line.uptrend.xyaxis"))
let second = UITabBarItem(title: "secondTab", image: UIImage(systemName: "arrow.left.and.right.square"), selectedImage: UIImage(systemName: "arrow.left.and.right.square"))

let tabBar = UITabBar()
tabBar.setItems([first, second], animated: true)
view.addSubview(tabBar)
tabBar.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
    tabBar.leadingAnchor.constraint(equalTo: view.leadingAnchor),
    tabBar.trailingAnchor.constraint(equalTo: view.trailingAnchor),
    tabBar.bottomAnchor.constraint(equalTo: view.bottomAnchor),
    tabBar.heightAnchor.constraint(equalToConstant: 100)
])

tabBar.isAccessibilityElement = false
tabBar.accessibilityTraits = .tabBar

However, my tabBar and tabBarItems are supposed to be UIView not UITabBar and UITabBarItems. And this doesn't work.

// Doesn't work
let label1 = UILabel()
let label2 = UILabel()
label1.text = "first"
label2.text = "second"

let stackView = UIStackView(arrangedSubviews: [label1, label2])
view.addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
  stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
  stackView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
stackView.backgroundColor = .cyan
stackView.axis = .horizontal

stackView.isAccessibilityElement = false
stackView.accessibilityTraits = .tabBar
Joey
  • 31
  • 3
  • Did you try it on a device? I read that on simulator it doesn't work as expected. https://www.basbroek.nl/custom-tab-bar-accessibility – Andreas Jul 06 '22 at 09:52
  • @Joey As is, you have just two views embedded in a stack view. If you add the 'tabbar' trait, it's just an indication for VoiceOver to know what this object is BUT it won't incorporate all its properties and behavior... it's up to you to define them. – XLE_22 Jul 06 '22 at 12:33

0 Answers0