Skip to Content

How do I get a color scheme in SwiftUI?

Choosing colors is an important part of app design. A good color scheme creates visual harmony, directs attention, and conveys meaning. SwiftUI makes it easy to define and use a color scheme in your app.

What is a color scheme?

A color scheme is a set of colors used throughout an app or design. The colors are chosen to work well together while serving different purposes. A basic color scheme often includes:

  • Primary color – Used for key elements like headers and buttons.
  • Secondary color – Used for accents and compliments the primary.
  • Background colors – Used for app backgrounds and container views.
  • Text colors – Chosen to contrast backgrounds and other elements.

Additional colors are often defined for specific purposes like error messages, warnings, and highlights. Using a limited set of thoughtfully chosen colors creates visual consistency in an app.

Why use a color scheme?

Here are some of the benefits of defining and applying a color scheme:

  • Consistency – Using the same colors for the same purposes throughout the app provides familiarity for users.
  • Meaning – Colors can reinforce the meaning of elements, like red for errors.
  • Focus – A distinct primary color attracts attention to key elements.
  • Harmony – Colors chosen to complement each other create pleasant designs.
  • Branding – A color scheme can reflect and strengthen your brand identity.
  • Accessibility – Proper contrast between colors helps support accessibility.

Taking the time to thoughtfully define a color scheme pays off through a more intentional, polished app design.

How to define a color scheme in SwiftUI

SwiftUI makes it easy to create and use a color scheme. Here are the basic steps:

  1. Define color variables – Use Swift extensions like Color and UIColor to define your palette.
  2. Apply colors – Use the variables throughout views for elements like text, backgrounds, and more.
  3. Support dark mode – Adapt colors for both light and dark appearances.
  4. Override colors – Allow dynamic theme colors to take precedence.

Let’s look at each of these steps in more detail.

1. Define color variables

Start by defining Color or UIColor values for each of the colors in your scheme:


// Light mode colors
extension Color {
  static let primary = Color("PrimaryColor")
  static let secondary = Color("SecondaryColor")
  static let background = Color("BackgroundColor")
}

// Dark mode colors  
extension UIColor {
  static let primaryDark = UIColor { traitCollection in
    // Adapt for dark mode
  }
  static let secondaryDark = UIColor { traitCollection in
    // Adapt for dark mode 
  }
  // Other dark mode colors
}

Defining these as static variables lets you reuse them easily. Use appropriate names that reflect their purpose, like “primary”, “secondary”, “background”, etc.

2. Apply the color scheme

With your color variables ready, you can start using them throughout your SwiftUI views:


struct ContentView: View {

  var body: some View {
    VStack {
      Text("Title")
        .foregroundColor(.primary)
        
      Button("Tap Me")
        .background(.secondary)
  
      Image("Logo")
        .foregroundColor(.secondary)
    }
    .background(.background)
  }
}

Use them for text, backgrounds, borders, images, and any other views. This lets you quickly apply your scheme for visual consistency.

3. Support dark mode

To make your colors adapt for dark mode, use separate definitions for light and dark appearances:


// Light 
extension Color {
 // light mode colors
}

// Dark
extension UIColor { 
  // dark mode colors 
}

Then modify the dark values to work well on black backgrounds. Test your scheme in both modes.

4. Allow dynamic colors

Make sure any dynamic colors like .accentColor can override your scheme when needed:

 
Text("Hello")
  .foregroundColor(.accentColor)

This allows users to set their own theme preferences while keeping your scheme as defaults.

Example color schemes

Here are some example color schemes for inspiration:

Scheme Primary Secondary Background
Blue modern Deep blue Light blue White
Eco green Dark green Light green Beige
Colorful Violet Orange Light gray

Use a tool like Adobe Color to find attractive color combinations. Consider your brand, audience, and style for the best fit.

Tips for choosing colors

Keep these tips in mind as you define your palette:

  • Pick 2-5 colors to keep it simple
  • Make sure colors complement each other
  • Use proper contrast for text legibility
  • Test with light and dark modes
  • Review on actual devices
  • Check that colors meet accessibility standards
  • Consider conveying meaning with colors (like red for errors)

Choosing the right colors takes trial and error. The most important thing is creating sufficient contrast while coordinating colors. Reviewapple’s human interface guidelines for more details.

Updating your color scheme

If you need to modify your color scheme later, simply update the variables you defined:


extension Color {
  static let primary = Color("NewPrimaryColor")
  
  // Additional updates
}

Then changes will flow throughout your app automatically. This avoids updating colors individually in many views.

Conclusion

Defining and applying a color scheme is an important part of SwiftUI app design. Start by choosing colors that work together while serving distinct roles. Use variables to define the colors once and reuse them consistently for efficiency and harmony. Support dark mode variations and allow dynamic colors as needed. Taking the time to craft a thoughtful color scheme pays off in a polished, cohesive app identity.

With SwiftUI’s tools, it’s simple to implement a color scheme that brings intuitive, accessible designs to life. A unified palette helps direct focus, convey meaning, support branding, and look great in any mode. Making color an integral part of your app plan from the start results in a more intentional, cohesive experience.