Search code examples
iosswiftuiaccessibilityswiftui-navigationviewuiaccessibility

accessibilityViewIsModal in swiftUI


I've used accessibilityViewIsModal - true in UIKit where I need to shift accessibility focus from one view to another subview. How to use accessibilityViewIsModal in swiftUI?

I've tried following techniques to shift accessibility focus from one view to another subview.

  • UIAccessibility.post(notification: .screenChanged, argument: "New Screen appear")

Result: Accessibility focus didn't change

  • accessibilityViewIsModal = true

Result: Compile time error: Cannot find 'accessibilityViewIsModal' in scope

if showSelectPolicyView {
                SelectPolicyView(input: $inputData, showSelectPolicyView: $showSelectPolicyView)
                    .transition(.move(edge: .bottom))
                    .clipped()
                    .zIndex(1)
                    .background(Color.gray)
                    .onAppear {
                        accessibilityViewIsModal = true
                    }.onDisappear() {
                        accessibilityViewIsModal = true
                    }
            }

Where and how to use accessibilityViewIsModal in swiftUI ?


Solution

  • isModal is one of the accessibility traits you can add to a view, using accessibilityAddTraits.

    Use this trait to restrict which accessibility elements an assistive technology can navigate. When a modal accessibility element is visible, sibling accessibility elements that are not modal are ignored.

    That sounds exactly like what the documentation for accessibilityViewIsModal says.

    Usage:

    SomeView()
        .accessibilityAddTraits(.isModal)