Saturday, November 21, 2026
HomeiOS Developmentios - Pairing two components for accessibility functions with .accessibilityLabeledPair(function:id:in:) in SwiftUI

ios – Pairing two components for accessibility functions with .accessibilityLabeledPair(function:id:in:) in SwiftUI

[ad_1]

Within the “Accessibility in SwiftUI” session from WWDC19, the presenter mentioned (20min): “In an upcoming seed of SwiftUI you can (…) arrange a connection between the label on the left and the popup button on the correct.”

Checking on the obtainable APIs, I assume that is performed with the .accessibilityLabeledPair(function:id:in:) modifier. I have not been in a position to make it work. In my thoughts, this may work equally to labelFor in Android? Has anybody used this modifier efficiently?

Utilizing some code just like the one under, the place I first give a textual content view the label function and a button the content material one. After which each the identical Id and namespace. I’d count on VoiceOver to deal with each components as one and for the accessibility label to be the textual content within the textual content view. As a substitute, for me, this behaves precisely the identical as if I do not apply the modifier. VoiceOver focuses on the textual content first and, after a swipe to the correct, it focuses on the button. Am I misunderstanding how this could work?

import SwiftUI

struct ContentView: View {
    @Namespace var namespace
    @State personal var isOn = false

    var physique: some View {
        HStack {
            Textual content("I agree on the phrases and situations")
                .accessibilityLabeledPair(function: .label, id: "aPair", in: namespace)
            Button {
                isOn = !isOn
            } label: {
                isOn ? Picture(systemName: "checkmark.circle.fill") : Picture(systemName: "circle")
            }
            .accessibilityLabeledPair(function: .content material, id: "aPair", in: namespace)
        }
    }
}

Thanks everybody!

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments