Since foregroundColor
will be/is now deprecated I was trying to use the recommended alternative foregroundStyle
but when I try to pass .accentColor
as a parameter it shows this error:
Why did it work for foregroundColor
? and what would be the best way to use .accentColor
now?
Thanks for any info :)
foregroundStyle
takes a ShepeStyle
whereas foregroundColor
takes a Color
. As you can see in the documentation, ShapeStyle
indeed doesn't declare anything called accentColor
, so you cannot pass .accentColor
.
You can pass Color.accentColor
though, because Color
conforms to ShapeStyle
. Note that Color.accentColor
is different from .accentColor
in this context. Without the Color
prefix, Swift tries to find a accentColor
in ShapeStyle
, because that's what foregroundStyle
takes. See also Implicit Member Expression from the language reference.
If you want to add accentColor
to ShapeStyle
, you can:
extension ShapeStyle where Self == Color {
static var accentColor: Color { Color.accentColor }
}
But also consider using .tint
, which is a member of ShapeStyle
. Documentation says:
You can set the tint color with the
tint(_:)
modifier. If no explicit tint is set, the tint is derived from the app’s accent color.
Since it says "derived from", presumably this ShapeStyle
is not exactly Color.accentColor
- SwiftUI might make slight changes to it in different contexts to make it better "fit" its surroundings.