Defining Custom Colors In Swift

This post illustrates two ways you can define custom colors in Swift code.


Here is an example using Structs.

In the case of having multiple shades of a color, you can use nested Structs to group them as I’ve shown with blue.

UIColor Extension

You can instead elect to define an extension of UIColor to define custom colors.

Using either of these styles, you can place the code in any Swift file and use the custom color throughout your project. Here is an example of creating the defined colors.

If you want to see this code in a playground, I’ve added it to my SwiftNotes project in GitHub.

References: Photo from pixabay

Leave a Comment: