I currently have a ButtonStyle defined as below:
struct RoundedButtonRed: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
HStack {
Spacer()
configuration.label.foregroundColor(.white)
Spacer()
}
.padding()
.background(Color.red.cornerRadius(13.0))
.scaleEffect(configuration.isPressed ? 0.85 : 1)
}
}
I also have another struct (RoundedButtonBlack) that has a .background(Color.black.cornerRadius(13.0)), which seems like unnecessary code duplication. Is there a way to add custom configuration options so that I can use the same struct for different colors?
e.g.
struct RoundedButtonRed: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
HStack {
Spacer()
configuration.label.foregroundColor(.white)
Spacer()
}
.padding()
.background(configuration.backgroundColor) // background color taken from configuration or passed into .buttonStyle(RoundedButton()) call
.scaleEffect(configuration.isPressed ? 0.85 : 1)
}
}
Or, perhaps there is a better way altogether?
