Defining dynamic colors in Swift

Defining dynamic colors in Swift

For the most part, it’s fair to say that modern iOS and Mac apps are expected to gracefully adapt to whether the user’s device is running in light or dark mode, which often requires us to use more dynamic colors within the UIs that we build.

While Apple does make it fairly straightforward to declare such dynamic colors using Xcode’s asset catalog system, sometimes we might want to define our colors inline within our Swift code instead. So let’s take a look at a few ways to do just that, using either SwiftUI or UIKit.

[…]

What’s really great is that SwiftUI’s Color and UIColor can easily be bridged — meaning that we can also make the above solution fully SwiftUI-compatible with very little extra code:

[…]