Adapting to Light & Dark Mode
Use semantic colors for both appearances.
Adapting to Light & Dark Mode is a free SwiftUI Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Appearances
iOS offers Light and Dark Mode, and users switch any time. A polished app looks great in both without extra effort.
Use Semantic Colors
Reach for semantic colors like Color.primary or Color(.systemBackground). They flip automatically between appearances.
Text("Title")
.foregroundStyle(.primary)
.background(Color(.systemBackground))Avoid Hardcoded Colors
A fixed Color.black background stays black in Dark Mode and can vanish into it. Semantic colors adapt; raw ones do not.
Text("Hi")
.background(.black)Asset Catalog Colors
Define a color in the asset catalog with separate Any and Dark values. Reference it by name and it switches for you.
Color("Brand")Primary and Secondary
Use .secondary for less important text. It dims appropriately in each mode while staying readable.
Text("Subtitle")
.foregroundStyle(.secondary)Materials for Depth
System materials like .ultraThinMaterial blur the background and adapt to the mode, giving cards a native feel.
VStack { }
.background(.ultraThinMaterial)Detecting the Mode
Read the current appearance with @Environment(\.colorScheme) when you truly need different content per mode.
@Environment(\.colorScheme) var schemeBranch on Scheme
With colorScheme in hand, you can choose an asset, like swapping a logo, only where a semantic color can't do the job.
Image(scheme == .dark ? "logoDark" : "logoLight")Preview Both Modes
Add .preferredColorScheme(.dark) to a preview to check Dark Mode without leaving Xcode.
#Preview {
ContentView()
.preferredColorScheme(.dark)
}Forcing a Mode
You can lock a screen with preferredColorScheme, but prefer respecting the user's system choice whenever you can.
ContentView()
.preferredColorScheme(.dark)Check Contrast
Verify text stays readable in both modes. Good contrast is part of accessibility, not just style.
Quick Check
Which color choice automatically adapts between Light and Dark Mode?
Recap
Your app now looks right in any appearance: lean on semantic colors, asset colors, and materials, and preview both modes. 🌓
Frequently asked questions
Is the “Adapting to Light & Dark Mode” lesson free?
Yes — the full text of “Adapting to Light & Dark Mode” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “Adapting to Light & Dark Mode”?
Use semantic colors for both appearances. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Adapting to Light & Dark Mode” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- VoiceOver & Accessibility Labels
- Dynamic Type & Scaled Fonts
- Localizing Strings & String Catalogs
- Adapting to Light & Dark Mode