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

后端 未结 3 1363
孤街浪徒
孤街浪徒 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)
        }
    }
    

提交回复
热议问题