The workaround of writing the entire literal seems to work when assigning the color to a variable or constant, at least in Xcode 15.2 (15C500b). However, be aware that if the # isn't initially omitted from #colorLiteral, you'll be typing blind for a while.
But it doesn't work in all contexts, for example it does not work when trying to use a color literal to set the background color of a view.
struct myView: View {
var body: some View {
Text("Hello world!")
.background(Color(#colorLiteral(red: 0.3, green: 0.1, blue: 0.2, alpha: 1.0))) // shows as text
}
}
An additional workaround is to first assign the color to a constant, then use that constant.
struct anotherView: View {
let bgColor = Color(#colorLiteral(red: 0.3, green: 0.1, blue: 0.2, alpha: 1.0)) // shows as a color swatch
var body: some View {
Text("Hello world!")
.background(bgColor)
}
}
Topic:
Developer Tools & Services
SubTopic:
Xcode
Tags: