Hi Conrad
I think in this forum there are a lot of guys learning stuff. Everybody has to start somewhere.
What Nigel suggests is true and probably what you are looking for: You want to use a predefined color provided by Apple. Therefore .orange
and .red
(as a shortcut for Color.orange
and Color.red
).
But if you want to use a custom color which you have defined in the Assets.xcassets
as a "Color Set", you have to use the String-based initializer as you did. If you check the documentation for Color you see that this initializer takes a Bundle
parameter, therefore indicating the source location for this externally defined color.
Further, the issue with your Rectangle()
example is, that it also has a fill color (as you didn't specify any it uses black) which covers completely your background gradient you specified. So to make it visible I suggest to stroke the Rectangle to make the background visible:
// Not working gradient: "Orange" and "Red" are not defined
Rectangle()
.stroke(Color.gray)
.frame(height: 20.0)
.background(LinearGradient(gradient: Gradient(colors: [Color("Orange"), Color("Red")]), startPoint: .leading, endPoint: .trailing))
// Working gradient: .orange and .red defined by SwiftUI
Rectangle()
.stroke(Color.gray)
.frame(height: 60.0)
.background(LinearGradient(gradient:
Gradient(colors: [.orange, .red]),
startPoint: .leading,
endPoint: .trailing))
I hope this helps you understand the issue.
regards
Philipp