Thursday, May 9, 2024
HomeiOS Developmentios - SwiftUI - Accessibility set deal with navigation title

ios – SwiftUI – Accessibility set deal with navigation title


I am making an attempt to set the VoiceOver focus to be on the navigation title when the consumer first opens a web page. I’ve performed this with UIKit utilizing the UIAccessibility publish(notification:argument:) operate, however not sure learn how to accomplish this with SwiftUI.

What I’ve tried to do is make a customized ToolbarContent struct in order that I can add the .accessibilityFocused view modifier to the title itself. Nevertheless this doesn’t appear to truly give it focus. My worry is that as a result of it is a ToolbarContent struct and never a normal View struct, it is inflicting some points.

This is my code for the ToolbarContent, and in an effort to make it extra reusable I’ve additionally added a ViewModifier for that:

struct EnhancedTitleView: ToolbarContent {
    let title: String
    @AccessibilityFocusState var isTitleFocused: Bool
    
    var physique: some ToolbarContent {
        ToolbarItem(placement: .principal) {
            VStack {
                Textual content(title)
                    .accessibilityFocused($isTitleFocused) // NOTE: this doesn't work
            }
            .job {
                 isTitleFocused = true
            }
        }
    }
}

struct EnhancedTitleViewModifier: ViewModifier {
    let title: String

    func physique(content material: Content material) -> some View {
        return content material
            .toolbar {
                EnhancedTitleView(title: title)
            }
    }
}

extension View {
    func enhancedNavigationTitle(_ title: String) -> some View {
        return modifier(EnhancedTitleViewModifier(title: title))
    }
}

And ideally it may be used like this in a standard view:

struct TestView: View {
    var physique: some View {
        VStack {
            Textual content("Testing")
        }
        .enhancedNavigationTitle("Testing View")
    }
}

Any assist can be appreciated



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments