CodexBloom - Programming Q&A Platform

Struggling with WCAG compliance for custom UI components in SwiftUI on iPhone

๐Ÿ‘€ Views: 153 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-10-08
swiftui accessibility wcag swift

I've been researching this but Recently started working with a project that requires strict adherence to WCAG 2.1 guidelines. My focus is on creating custom UI components in SwiftUI, but I'm hitting a wall when it comes to implementing accessibility features correctly. For instance, I'm trying to ensure that all interactive elements are accessible via VoiceOver. Here's a snippet of my code for a custom button: ```swift struct AccessibleButton: View { var action: () -> Void var label: String var body: some View { Button(action: action) { Text(label) .font(.headline) .padding() .background(Color.blue) .foregroundColor(.white) .cornerRadius(10) } .accessibilityLabel(label) .accessibilityHint("Taps to perform action") } } ``` I've added the `accessibilityLabel` and `accessibilityHint`, but it seems like there's still something off. When testing with VoiceOver, it sometimes reads more than I expect, or skips the button altogether. In search of a solution, I tried using `.accessibilityAddTraits(.isButton)` but that didnโ€™t seem to make a difference. Additionally, I noticed that when I group multiple buttons together, VoiceOver reads them as one single action rather than individual buttons, which complicates usability. To address this, I've been looking into using `accessibilitySortPriority()` to manage the reading order, but it's not entirely clear how to implement it effectively for a list of buttons. Hereโ€™s how Iโ€™m trying to set it up: ```swift HStack { AccessibleButton(action: { print("First button tapped") }, label: "First") .accessibilitySortPriority(1) AccessibleButton(action: { print("Second button tapped") }, label: "Second") .accessibilitySortPriority(2) } ``` Still, the results seem inconsistent. Are there best practices or common pitfalls I might be overlooking? Any insights on ensuring full compliance with accessibility standards while maintaining a seamless user experience would be greatly appreciated. Targeting iPhone models and testing on iOS 17, I want to ensure that users with disabilities get the best possible experience. For context: I'm using Swift on Debian. I'm open to any suggestions.