Color literal not displaying as Color Swatch?

Hi, should color literals work inside Views, I am sure this used to work?

Xcode Version 13.0 (13A233)

Note, myColor works, but the same literal in the fill does not.

let myColor = Color(#colorLiteral(red: 0.292, green: 0.081, blue: 0.6, alpha: 255))

struct TestView: View {
  var body: some View {
    RoundedRectangle(cornerRadius: 25)
      .fill(Color(#colorLiteral(red: 0.292, green: 0.081, blue: 0.6, alpha: 255)))
      .frame(width: 100, height: 100)
       
  }
}
Post not yet marked as solved Up vote post of OnionSource Down vote post of OnionSource
4.7k views

Replies

Your code (with the colorLiteral inside the View) works for me.
Xcode 13.0 (13A233)

Hiya, when you say "works for me" you mean that it's displaying the literals correctly in the IDE (i.e. showing two color literals as in the example "B" below). Just checking ...

You asked:

should color literals work inside Views?

So I:

• Copied your code (above... the text, not the screenshot)
• Pasted it into an app
• Ran the app
• The color literal worked, inside a View

I now think you are asking something else. I think you are asking:

In Xcode, is the code...
Color(#colorLiteral(red: 0.292, green: 0.081, blue: 0.6, alpha: 255)
...replaced with a block of color

If I have understood that correctly, my answer to that is "no".

My question is should the Xcode IDE be displaying a color swatch 🟥 in the View shown, instead of just converting it to text "Color(#colorLiteral(red: 0.292, green: 0.081, blue: 0.6, alpha: 255))"

Hello, I have the same problem. Is this resolved?

I have the same problem. Is this resolved?

Not as of Xcode 13.2 Beta2, color literals display as a swatch in some places, but not others.

What I am seeing is this:

What I would like to see is this:

  • I have the same problem. I searched a lot and couldn't find a solution. Version 13.1 (13A1030d)

Add a Comment

i saw this on the stackoverflow comments of a related issue, i tried it and it worked for me... you have to actually type out #colorLiteral() and it will turn it into the color picker that you're expecting...

var color = #colorLiteral() //at this point it will turn this #colorLiteral into a broken image icon, but that's your color picker... and it'll look right after you select a color.
Rectangle().fill(Color(color)) // then just pass in the color you instantiated above
code-block

At the end it should look something like this:

I want to confirm for anyone else having this "issue" that:

-> as stated and screenshoted above, if you first declare your color as a constant, #colorLiteral() will work with the color picker (works like this in Xcode 13.3)

I can confirm that this issue still not resolved even in the latest Xcode version(13.3.1)