SwiftUI
UIColor
iOS development
Swift programming
iOS UI customization

How can I change a SwiftUI Color to UIColor?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When SwiftUI and UIKit live in the same app, color values often need to cross framework boundaries. A SwiftUI view may define a Color, while a UIKit API such as UINavigationBarAppearance, CALayer, or a third-party UIKit component expects UIColor.

On modern Apple platforms, the simplest conversion is usually direct: initialize UIColor from Color. The main thing to understand is that some colors are dynamic and resolve differently depending on traits such as light mode and dark mode.

Direct Conversion

On iOS 14 and later, UIKit can create a UIColor from a SwiftUI Color.

swift
1import SwiftUI
2import UIKit
3
4let swiftUIColor = Color.blue
5let uiColor = UIColor(swiftUIColor)
6print(uiColor)

That works well for ordinary colors and for many semantic colors. If you are configuring UIKit appearance from SwiftUI, an extension can keep the call sites tidy.

swift
1import SwiftUI
2import UIKit
3
4extension Color {
5    var asUIColor: UIColor {
6        UIColor(self)
7    }
8}
9
10let accent = Color(red: 0.18, green: 0.45, blue: 0.82)
11let navigationBarColor = accent.asUIColor

This is the standard answer when you control both frameworks and your deployment target is new enough.

Using the Result in UIKit

A common use case is styling a UIKit component from a color chosen in SwiftUI.

swift
1import SwiftUI
2import UIKit
3
4final class ThemePreviewController: UIViewController {
5    override func viewDidLoad() {
6        super.viewDidLoad()
7
8        let themeColor = UIColor(Color.orange)
9        view.backgroundColor = themeColor
10        navigationController?.navigationBar.tintColor = themeColor
11    }
12}

You can also bridge the other direction with Color(uiColor:) if UIKit remains the source of truth.

If the color is custom and shared throughout the app, it is often better to store it in a common theme abstraction instead of converting ad hoc in many places.

Dynamic and Semantic Colors

The conversion can get more subtle when the SwiftUI Color is semantic, such as Color.primary, or when it is sourced from an asset catalog with light and dark variants. The resulting UIColor can remain dynamic rather than becoming a single fixed RGBA value.

That is usually what you want. Dynamic colors allow UIKit controls to adapt automatically when traits change.

If you need the resolved channel values for a specific appearance, resolve the color against a trait collection.

swift
1import SwiftUI
2import UIKit
3
4let dynamicColor = UIColor(Color("BrandBackground"))
5let darkTraits = UITraitCollection(userInterfaceStyle: .dark)
6let resolved = dynamicColor.resolvedColor(with: darkTraits)
7
8var red: CGFloat = 0
9var green: CGFloat = 0
10var blue: CGFloat = 0
11var alpha: CGFloat = 0
12resolved.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
13print(red, green, blue, alpha)

That pattern matters if you are exporting a concrete value to Core Graphics, serializing theme data, or feeding an API that cannot work with dynamic colors.

Deployment and Compatibility Notes

If you support older OS versions, check whether the direct initializer is available in your deployment range. For newer iOS projects this is rarely an issue, but mixed-version codebases should still guard availability explicitly.

swift
1if #available(iOS 14.0, *) {
2    let uiColor = UIColor(Color.green)
3    print(uiColor)
4}

If the app already uses shared named colors from an asset catalog, referencing the asset from both SwiftUI and UIKit is often cleaner than converting at runtime repeatedly.

Common Pitfalls

A common mistake is assuming every converted color is a fixed RGBA value. Many colors are dynamic and only resolve fully in a given environment. If you inspect components without resolving the trait collection, the result may be misleading.

Another issue is trying to use conversion patterns backwards. UIColor(Color.someValue) is for SwiftUI to UIKit. If UIKit is your source, use Color(uiColor:) instead.

Developers also sometimes overengineer the problem by manually extracting components from SwiftUI colors. That is usually unnecessary on supported OS versions because Apple already provides the bridge.

Finally, do not forget platform availability. If your target includes older systems, wrap the conversion in an availability check or central helper.

Summary

  • On modern iOS versions, convert with UIColor(Color.someColor).
  • An extension can make repeated conversions cleaner.
  • Semantic and asset-based colors may stay dynamic after conversion.
  • Resolve against a trait collection when you need fixed RGBA values.
  • If both frameworks share the same asset catalog, direct asset reuse can be simpler than repeated bridging.

Course illustration
Course illustration

All Rights Reserved.