Search code examples
swiftmacosaccessibilityvoiceover

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!


Solution

  • 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.