SwiftUI ButtonStyle - how to check if button is disabled or enabled?

后端 未结 3 1332
孤街浪徒
孤街浪徒 2021-01-01 23:27

To style a button in SwiftUI, according to my understanding, you extend ButtonStyle and implement func makeBody(configuration: Self.Configuration) -> s

相关标签:
3条回答
  • 2021-01-02 00:03

    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)
        }
    }
    
    0 讨论(0)
  • 2021-01-02 00:11

    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.

    0 讨论(0)
  • 2021-01-02 00:24

    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())
        }}
    
      }
    
    0 讨论(0)
提交回复
热议问题