简体   繁体   中英

How to Group UI Elements in Swift for VoiceOver on MacOS?

I used NSStackView to easily organized UI elements and place them on different areas of main view.

Now, VoiceOVer sees all UI elements in one layer, and I'm wondering How I can make stack views so that VoiceOver users have to interact and stop interact with each stack view.

Even better, is there any generic view that automatically appears to VoiceOver as a group, and I can just use it as a container?

I'd really appreciate if someone could provide any snippet in Swift.

Thanks!

I was able to get NSStackView to work as a group by adopting NSAccessibilityGroup.

class GroupView:NSStackView, NSAccessibilityGroup {
    override func accessibilityLabel() -> String? {
        return "Test"
    }
}    

Then select the stack view from the storyboard, open identity inspector, and assign GroupView as the custom class.

This doesn't require subclassing. Just call:

stackView.setAccessibilityElement(true)

And then you should also add some information for VoiceOver users about the contents of the new group they will encounter. Example:

stackView.setAccessibilityLabel("actions")
stackView.setAccessibilityRole(.toolbar)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM