Change buttonStyle Modifier based on light or dark mode in SwiftUI

前端 未结 2 1029
不知归路
不知归路 2021-01-07 01:41

I want to set Custom buttonStyle modifier for button for light and dark mode. How to change buttonStyle Modifier based on light or dark mode? I want to set Cus

2条回答
  •  独厮守ぢ
    2021-01-07 02:06

    Just put that condition inside button style modifier, like

    // ... other your code
    })
    .buttonStyle(CustomButtonStyle(scheme: colorScheme)) // << here !!
    

    and in custom style

    struct CustomButtonStyle: ButtonStyle {
        var scheme: ColorScheme              // << here !!
    
        func makeBody(configuration: Self.Configuration) -> some View {
            configuration.label
            .padding(10)
                Group {
                    if configuration.isPressed {
                        Circle()   // example of internal dependency on scheme
                            .fill(self.scheme == .dark ? Color.offBlack :  Color.offWhite)
    
        // .. other code here
    }
    

提交回复
热议问题