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

后端 未结 3 1333
孤街浪徒
孤街浪徒 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条回答
  •  -上瘾入骨i
    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())
        }}
    
      }
    

提交回复
热议问题