What are the differences between accentColor and foregroundColor in SwiftUI?

Viewed 93

I don't know why there are both accentColor and foregroundColor properties in SwiftUI. I can't see their differences. For example:

Button(action: {}, label: {Text("Button")}).accentColor(.red)
Button(action: {}, label: {Text("Button")}).foregroundColor(.red)

The two buttons look the same!

1 Answers

Both yield the same result when applied to a Button. However, you can retrieve the accent color of the current context more easily using Color.accentColor as described here:

VStack(spacing: 20) {
    Text("Default Accent Color")
        .foregroundStyle(Color.accentColor)
    Group {
        Text("Overridden Accent Color")
            // will be red
            .foregroundStyle(Color.accentColor)
    }
    .accentColor(.red)
}
Related