Fix: Cannot Convert UIColor to Color When Using Hex Values in SwiftUI

Fix: Cannot Convert UIColor to Color When Using Hex Values in SwiftUI

Written by

in

Quick answer: SwiftUI’s Color type doesn’t accept UIColor directly. Add a Color extension that parses hex strings and calls Color’s sRGB initializer with red, green, blue, and opacity parameters. The extension should handle 3-digit (RGB), 6-digit (RRGGBB), and 8-digit (AARRGGBB) formats.

Why this happens

SwiftUI’s Color is a value type designed for cross-platform rendering, while UIColor is UIKit-specific. When you write .color(UIColor(...)), the compiler rejects it because the color(_:) modifier expects a Color?, not a UIColor.

Even when you create a Color extension, the initializer signature differs from UIKit. UIColor uses init(red:green:blue:alpha:), but Color requires init(_ colorSpace:colorSpace: red:green:blue:opacity:). The first parameter is the color space (usually .sRGB), and the last parameter is named opacity, not alpha.

How to fix it

1. Add a comprehensive Color(hex:) extension (recommended)

This version handles the three common hex formats, trims non-hex characters, and falls back to transparent black for invalid input.

import SwiftUI

extension Color {
    init(hex: String) {
        let hex = hex.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
        var int: UInt64 = 0
        Scanner(string: hex).scanHexInt64(&int)
        let a, r, g, b: UInt64
        switch hex.count {
        case 3: // RGB (12-bit) like "FFF"
            (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
        case 6: // RRGGBB (24-bit) like "FF00AA"
            (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
        case 8: // AARRGGBB (32-bit) like "80FF00AA"
            (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
        default:
            (a, r, g, b) = (1, 1, 1, 0) // transparent black fallback
        }

        self.init(
            .sRGB,
            red: Double(r) / 255,
            green: Double(g) / 255,
            blue: Double(b) / 255,
            opacity: Double(a) / 255
        )
    }
}

Usage:

Text("Hello")
    .foregroundStyle(Color(hex: "#FF5733"))        // 6-digit
Text("World")
    .foregroundStyle(Color(hex: "F00"))            // 3-digit
Text("!")
    .foregroundStyle(Color(hex: "80FF5733"))       // 8-digit with alpha

2. Use a UInt-based initializer for compile-time constants

If you prefer passing hex as integer literals (e.g., 0xFF5733), this shorter variant avoids string parsing at runtime.

extension Color {
    init(hex: UInt, alpha: Double = 1) {
        self.init(
            .sRGB,
            red: Double((hex >> 16) & 0xFF) / 255,
            green: Double((hex >> 8) & 0xFF) / 255,
            blue: Double(hex & 0xFF) / 255,
            opacity: alpha
        )
    }
}

Usage:

Color(hex: 0xFF5733)
Color(hex: 0xFF5733, alpha: 0.5)

3. Bridge UIColor when you must interoperate with UIKit APIs

If you receive a UIColor from a legacy API and need a Color, convert via CGColor components.

extension Color {
    init(uiColor: UIColor) {
        var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
        uiColor.getRed(&r, green: &g, blue: &b, alpha: &a)
        self.init(.sRGB, red: Double(r), green: Double(g), blue: Double(b), opacity: Double(a))
    }
}

How to prevent it

  • Keep the hex extension in a shared Color+Hex.swift file so every target can import it.
  • Prefer the String-based initializer for dynamic values (user themes, server-driven UI).
  • Use the UInt initializer for design-system constants to catch typos at compile time.
  • Avoid scattering UIColor in SwiftUI code; convert once at the boundary.

FAQ

Does SwiftUI have a built-in hex initializer?

No. As of iOS 17 and macOS 14, Color only provides initializers for sRGB, DisplayP3, and platform-specific colors. You must supply your own hex parser.

Why does the 3-digit case multiply by 17?

A 3-digit hex like "F0A" expands to "FF00AA". Each 4-bit nibble (0–15) becomes an 8-bit channel (0–255) by multiplying by 17 (255 ÷ 15).

Can I use this in previews and widgets?

Yes. The extension uses only SwiftUI and Foundation, so it works in app targets, widget extensions, and Xcode previews without UIKit dependencies.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.