'SwiftUI ButtonStyle - how to check if button is disabled or enabled?
To style a button in SwiftUI, according to my understanding, you extend ButtonStyle and implement func makeBody(configuration: Self.Configuration) -> some View within which you start applying modifications to the configuration.label which is a reference to the Button view.
Now, besides the label field, ButtonStyle.Configuration has a boolean field for isPressed, but that's seems to be all.
How do I check if the button is enabled or disabled?
For example, I want to draw a border around the button, and I want the border to be blue if the button is enabled and gray if disabled.
My first guess is something like this:
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.overlay(
RoundedRectangle(cornerRadius: 4).stroke(configuration.isEnabled ? Color.blue : Color.gray, lineWidth: 1).padding(8)
)
}
But there's no isEnabled field.
The Apple-supplied PlainButtonStyle obviously has access to this information, as the doc comment in the header file that declares it says 'The style may apply a visual effect to indicate the pressed, focused, or enabled state of the button.'
/// A `Button` style that does not style or decorate its content while idle.
///
/// The style may apply a visual effect to indicate the pressed, focused,
/// or enabled state of the button.
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public struct PlainButtonStyle : PrimitiveButtonStyle {
.....
Is there a way to access this information?
EDIT:
Before you suggest closing this question, please read this:
There's a similar question asked a while ago. That question does not specify any context.
Here I have a specific context: creating a generic reusable button style.
The answer provided there does not suffice. I can't expect people to pass the disabled state to the style constructor. That's too much duplication of effort.
Can you imagine if people have to write code this way:
Button() { .... }.disabled(someExprssion).buttonStyle(MyCustomStyle(disabled: someExpression))
Clearly that's not desireable.
Also clearly the Apple supplied style has access to the information without requiring people to pass the disable state again to the style.
If you close the question, you will forever prevent anyone from providing a useful answer to this question on StackOverflow.
Please reconsider before you propose closing.
EDIT2:
I have a guess that if you can get the EnvironmentValues.isEnabled of the Button view then it might be the right value.
So another way to ask the question is: is it possible in SwiftUI to get the environment of a view that you didn't define yourself?
Solution 1:[1]
Tweaked up a bit on @hasen answer so as to make things more configurable
struct MyButtonStyle: ButtonStyle {
var foreground = Color.white
var background = Color.blue
func makeBody(configuration: ButtonStyle.Configuration) -> some View {
MyButton(foreground: foreground, background: background, configuration: configuration)
}
struct MyButton: View {
var foreground:Color
var background:Color
let configuration: ButtonStyle.Configuration
@Environment(\.isEnabled) private var isEnabled: Bool
var body: some View {
configuration.label
.padding(EdgeInsets(top: 7.0, leading: 7.0, bottom: 7.0, trailing: 7.0))
.frame(maxWidth: .infinity)
.foregroundColor(isEnabled ? foreground : foreground.opacity(0.5))
.background(isEnabled ? background : background.opacity(0.5))
.opacity(configuration.isPressed ? 0.8 : 1.0)
}
}
}
Usage
Button(action: {}) {
Text("Hello")
}.buttonStyle(MyButtonStyle(foreground: .white, background: .green))
Solution 2:[2]
You can access the enabled value through environment directly in the ButtonStyle, eg:
struct MyButtonStyle: ButtonStyle {
@Environment(\.isEnabled) var isEnabled
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.background(!isEnabled ? Color.black : (configuration.isPressed ? Color.red : Color.blue))
}
}
Solution 3:[3]
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)
}
}
Solution 4:[4]
Answering your question: yes, it is possible to get the Environment value you didn't pass yourself. SwiftUI uses the Environment heavily and a lot of Views depend on it without you even knowing it.
A ButtonStyle can access Environment too so you can achieve what you want as easy as:
struct SomeButtonStyle: ButtonStyle {
@Environment(\.isEnabled) var isEnabled: Bool
func makeBody(configuration: Configuration) -> some View {
configuration.label
.disabled(!self.isEnabled)
.opacity(self.isEnabled ? 1 : 0.5)
}
}
Solution 5:[5]
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())
}}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | anoop4real |
| Solution 2 | Jonathan. |
| Solution 3 | damirstuhec |
| Solution 4 | |
| Solution 5 | E.Coms |
