Accessibility state - SwiftUI
In SwiftUI, the AccessibilityTraits
attribute can be used to indicate the accessibility state. The traits isSelected
can be used to indicate the current state.
@State private var isSelected: Bool = false
var body: some View {
CustomCheckbox(isSelected: $isSelected)
.accessibilityAddTraits(isSelected ? [.isSelected] : [])
}
If your state is not isSelected
, we recommended using the accessibilityValue
view modifier to indicate the state.
@State private var isExpanded = false
var body: some View {
ExpandableView()
// Accessibility value indicates the state
.accessibilityValue(isExpanded ? "Expanded": "Collapsed")
}