Change buttonStyle Modifier based on light or dark mode in SwiftUI

前端 未结 2 1016
不知归路
不知归路 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
    }
    
    0 讨论(0)
  • 2021-01-07 02:11

    You could define a named color in Assets.xcassets with a variation for the dark mode:

    This works out of the box even in a ButtonStyle:

    Color("ButtonBorder")
    
    0 讨论(0)
提交回复
热议问题