History
Loading...
Loading...
September 2, 2025
Color.primary and Color.secondary instead of hardcoded colors like Color.black or Color.gray to automatically support both light and dark mode without additional code. These semantic colors adapt to the user's system appearance settings.This practice creates a centralized color system that combines custom branded colors with system-adaptive colors, ensuring consistent theming across your app while maintaining accessibility and dark mode support.
extension Color {
static let appPrimary = Color("AppPrimary")
static let appSecondary = Color("AppSecondary")
static let appBackground = Color("AppBackground")
// Fallback for older iOS versions
static let appText = Color.primary
static let appSubtext = Color.secondary
}
// Usage in views
struct ContentView: View {
var body: some View {
VStack {
Text("Main Title")
.foregroundColor(.appText)
Text("Subtitle")
.foregroundColor(.appSubtext)
}
.background(Color.appBackground)
}
}Centralized color management prevents inconsistencies, makes theme changes easier to implement, and ensures your app respects user preferences for accessibility and appearance. It also makes your code more maintainable when design requirements change.