amey rane
amey rane

Reputation: 315

SwiftUI Accessibility VoiceOver Focus Issue

I have one button on a screen and on tap of that button i am opening one modal (view). But after closing that view accessibility VoiceOver focus goes to top left element on the screen. I want that focus to stay on the same button after closing that view.

Upvotes: 4

Views: 1164

Answers (1)

AppleDeveloper
AppleDeveloper

Reputation: 1443

// State variable to control loading state
@State var isLoading: Bool = false
    
// Accessibility focus state variable to manage focus
@AccessibilityFocusState var isLoadingIndicatorFocused: Bool
    
var body: some View {
    VStack {
        Button("Search Appt website") {
            // Set loading state to true
            isLoading = true
            // Move focus to the loading indicator
            isLoadingIndicatorFocused = true
        }
            
        if isLoading {
            ProgressView()
                .accessibilityFocused($isLoadingIndicatorFocused)  // Bind the focus state
                .accessibilityLabel("Loading")  // Provide an accessible label
            }
        }
    }

ref: https://appt.org/en/docs/swiftui/samples/accessibility-focus#:~:text=Accessibility%20focus%20%2D%20SwiftUI,specific%20element%20in%20your%20app.

Upvotes: 1

Related Questions