2 Replies
      Latest reply on Aug 27, 2019 1:42 PM by yvsong
      yvsong Level 1 Level 1 (0 points)

        How to convert a UIColor to Color for use in SwiftUI?

        • Re: UIColor -> Color
          m_bedwell Level 2 Level 2 (70 points)

          You'll want to check the documentation, as this is shooting from memory, and has zero error checking or testing...but something like:


          func convertUIColor (uiColor: UIColor) -> Color {
              return Color(red: Double(uiColor.cgColor.components![0]), green: Double(uiColor.cgColor.components![1]), blue: Double(uiColor.cgColor.components![2]))

          One of the initializers for Color is a simple RGB value triplet, so extract the RGB values from the UIColor (I don't know of any other way except using CoreGraphics), and initialize your Color.  This won't take into account color space or opacity values though.

          • Re: UIColor -> Color
            yvsong Level 1 Level 1 (0 points)

            Color has a new

            init(_ color: UIColor)