To style a button in SwiftUI, according to my understanding, you extend ButtonStyle
and implement func makeBody(configuration: Self.Configuration) -> s
Another option would be to pass a disabled
Boolean value to your ButtonStyle
, and then use the allowsHitTesting
modifier on the label to effectively disable the button:
struct MyButtonStyle: ButtonStyle {
let disabled: Bool
func makeBody(configuration: Self.Configuration) -> some View {
configuration
.label
.allowsHitTesting(!disabled)
}
}
I found the answer thanks to this blog: https://swiftui-lab.com/custom-styling/
You can get the enabled state from the environment by creating a wrapper view and using it inside the style struct:
struct MyButtonStyle: ButtonStyle {
func makeBody(configuration: ButtonStyle.Configuration) -> some View {
MyButton(configuration: configuration)
}
struct MyButton: View {
let configuration: ButtonStyle.Configuration
@Environment(\.isEnabled) private var isEnabled: Bool
var body: some View {
configuration.label.foregroundColor(isEnabled ? Color.green : Color.red)
}
}
}
This example demonstrates how to get the state and use it to change the appearance of the button. It changes the button text color to red if the button is disabled or green if it's enabled.
You can merge the disabled status into the buttonStyle, so that you will not used it twice in the application.
struct CustomizedButtonStyle : PrimitiveButtonStyle {
var disabled: Bool = false
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label.disabled(disabled)
.overlay(
RoundedRectangle(cornerRadius: 4).stroke(disabled ? Color.blue : Color.gray, lineWidth: 1).padding(8)
)
}
}
struct ButtonUpdate: View {
var body: some View {
VStack{
Button(action: {
}) { Text("button")
}.buttonStyle(CustomizedButtonStyle(disabled: true))
Button(action: {
}) { Text("button")
}.buttonStyle(CustomizedButtonStyle())
}}
}