Accessibility Features (a11y) in iOS 18 - #30DaysOfSwift
Day 26: Making Apps Accessible for Users with Disabilities
Today, we’ll explore how to make your SwiftUI applications accessible to users with disabilities.
Ensuring accessibility means that all users can interact with your app effectively and enjoyably.
SwiftUI offers several accessibility modifiers to help improve the usability of your app for all users:
.accessibilityLabel: Provides a descriptive label for UI elements.
.accessibilityValue: Represents the current value of an interactive element.
.accessibilityHint: Offers hints about how to use a UI element.
.accessibilityAction: Allows you to define custom actions for accessibility.
Building an Accessible Form
Let’s create a simple form where users can input their name and age.
import SwiftUI
struct AccessibleFormView: View {
@State private var name: String = ""
@State private var age: String = ""
var body: some View {
Form {
Section(header: Text("User Information")
.font(.headline)
.accessibilityLabel("User Information Section")) {
TextField("Enter your name", text: $name)
.accessibilityLabel("Name")
.accessibilityValue(name.isEmpty ? "Empty" : name)
.accessibilityHint("Please enter your full name.")
TextField("Enter your age", text: $age)
.keyboardType(.numberPad)
.accessibilityLabel("Age")
.accessibilityValue(age.isEmpty ? "Empty" : age)
.accessibilityHint("Please enter your age as a number.")
}
Button(action: submit) {
Text("Submit")
.padding()
.frame(maxWidth: .infinity)
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(8)
}
.accessibilityLabel("Submit Button")
.accessibilityHint("Tap to submit your information.")
}
.padding()
.navigationTitle("Accessibility Form")
}
private func submit() {
// Handle form submission
print("Name: \(name), Age: \(age)")
}
}
After implementing accessibility features, it’s essential to test them with VoiceOver on your device.
Navigate through your app and ensure that all elements are announced correctly, and users can interact with them smoothly.
Best Practices for Accessibility
Use Dynamic Type for adjustable text sizes.
Ensure sufficient contrast between text and background colors.
Provide text labels for color-coded information.
Make interactive elements large enough for easy tapping.
Happy Coding!
Subscribe to my newsletter
Read articles from Vaibhav directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Vaibhav
Vaibhav
From product vision to pixel-perfect execution, I'm a seasoned Product Manager turned iOS App Developer, leveraging Swift and SwiftUI to craft intuitive, user-centric experiences.