How to create and use a Color Set in XCode

In the Assets, right click and select "New Color Set." Then define your light and dark mode colors using the inspector on the right hand side.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 10/16/2024 at 14:10
Last Updated on 10/16/2024 at 14:42
<p>Two flowers that represent SwiftUI and XCode side by side. Beneath them sits the text "Color Set."</p>

In the Assets, right click and select "New Color Set." Then define your light and dark mode colors using the inspector on the right hand side.

Subscribe

The example can be found on the main branch of our SwiftUI Open Source Starter Project linked below.

Download Open Source SwiftUI Starter Project

Step One: Create

<p>A screenshot of XCode showing how if you right click in the assets of XCode, you can create a new color set using the menu that pops up.</p>

Select the Assets folder in your project and under the definitions, right click and select New Color Set from the menu that pops up.

Step Two: Name

<p>A screenshot of XCode showing that after you select a create a New Color Set, XCode automatically lets you name the new color set. We have named it "Sample."</p>

Name the Color Set. Press the Enter key when done.

Please note that you must NOT name color sets "Red", "Green", "Primary" or "Background" as these conflict with the Apple System definitions. Apple has yet to provide a means for developers to alter the standard color scheme.

Step Three: Define

<p>A screenshot of XCode highlighting how you can set the color set, color variables.</p>

Select the relevant color (i.e. Any Appearance for Light mode or Dark for Dark Mode) and using the inspector on the right hand side, set the relevant color.

Step Four: Declare and use.

<p>A screenshot of XCode showing how we have declared the color within a Colors struct within our app.</p>

To use a Color Set, use the following syntax - where "COLOR_NAME" is the name that you set in step two:

Color("COLOR_NAME")

We recommend declaring the colors in a color struct and offering them as part of a styleguide.

Follow the tutorial below to learn more.

How to create a Styleguide in SwiftUI

Looking to learn more about SwiftUI, Swift, Design and Technology?

Search our blog to learn more about Swift, SwiftUI, design and technology.

Search our Blog

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe