
Here’s a quick example of how to make the new gradient
property of Color
more useful. What is returned is an AnyGradient, which can only be used as a FillStyle and not directly as a view. Unlike UIColor
, which was definitively not a UIView
in UIKit, Color
is as much a part of the hierarchy as a Text
or Button
. You’ll notice this difference in the way that the .background()
modifier takes any kind of view, but it is happy to receive a Color
.
The fact that this modifier doesn’t necessarily take a Color
leads to the annoying situation where .foregroundColor(.red)
is valid, but .background(.red)
is not because the underlying type cannot be assumed.
Color
is displayed similarly to a Rectangle
that has been given a foregroundColor
, by stretching the maximum amount horizontally and vertically. Setting the FillStyle
for a Rectangle
has a similar effect to setting its foregroundColor
, so .fill(Color.red)
could also work.
Since AnyGradient
can be a FillStyle
too, so this was all I needed to provide in the view body.
Now Color.red.gradient
can be used as a view just like Color.red
!