【问题标题】:Change buttonStyle Modifier based on light or dark mode in SwiftUI在 SwiftUI 中根据明暗模式更改 buttonStyle 修饰符
【发布时间】:2020-09-08 15:13:42
【问题描述】:

我想为明暗模式的按钮设置自定义 buttonStyle 修饰符。 如何根据亮或暗模式更改按钮样式修饰符?我想为我的按钮设置明暗模式的自定义修饰符。

这是我的按钮代码,

Button(action: {
    print("button tapped")

}, label: {
    LinearGradient(gradient: Gradient(colors: [.darkBlueColor, .lightBlueColor]), startPoint: .top, endPoint: .bottom)
        .mask(Image(systemName: "ellipsis")
            .resizable()
            .aspectRatio(contentMode: .fit)
    ).frame(width: iPhoneSE ? 26 : 25, height: iPhoneSE ? 26 : 25, alignment: .center)
})
.buttonStyle(lightButtonStyle())

struct lightButtonStyle: ButtonStyle {

    func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
        .padding(10)
        .background(
            Group {
                if configuration.isPressed {
                    Circle()
                        .fill(Color.offWhite)
                        .overlay(

                            Circle()
                                .stroke(Color.lightGray2, lineWidth: 4)
                                .blur(radius: 1)
                                .offset(x: 2, y: 2)
                                .mask(Circle().fill(LinearGradient(Color.black, Color.clear)))
                        )
                        .overlay(
                            Circle()
                                .stroke(Color.white, lineWidth: 4)
                                .blur(radius: 1)
                                .offset(x: -2, y: -2)
                                .mask(Circle().fill(LinearGradient(Color.clear, Color.black)))
                        )
                } else {
                    Circle()
                        .fill(Color.offWhite)
                        .shadow(color: Color.white.opacity(0.8), radius: 1, x: -2, y: -2)
                        .shadow(color: Color.lightPurple.opacity(0.6), radius: 1, x: 2, y: 2)
                }
            }
        )
    }
}

对于深色模式,我有另一个具有不同颜色和阴影的 buttonStyle。

我知道我们可以像这样更改其他修饰符,

.fill(colorScheme == .dark ? Color.darkEnd : Color.white)

但有些我无法更改 buttonStyle 修饰符。

【问题讨论】:

    标签: ios swift button swiftui ios-darkmode


    【解决方案1】:

    您可以在 Assets.xcassets 中为暗模式定义一个命名颜色:

    即使在 ButtonStyle 中也可以开箱即用:

    Color("ButtonBorder")
    

    【讨论】:

      【解决方案2】:

      只要把那个条件放在按钮样式修饰符里面,比如

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

      自定义样式

      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
      }
      

      【讨论】:

        猜你喜欢
        • 2020-01-30
        • 2020-10-12
        • 2023-04-05
        • 1970-01-01
        • 2020-04-28
        • 2021-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多