如果按钮在swiftUI中被禁用,如何更改按钮背景色

如果按钮在swiftUI中被禁用,如何更改按钮背景色,swiftui,Swiftui,我正在尝试创建一个按钮样式,如果该按钮被禁用,则该样式具有不同的背景颜色 我该怎么做? 我已经创建了下面的代码来响应我自己介绍的变量,但是是否可以让它在按钮上进行响应。disabled()状态 我的代码: struct MyButtonStyle: ButtonStyle { var enabledState = false func makeBody(configuration: Self.Configuration) -> some View { co

我正在尝试创建一个按钮样式,如果该按钮被禁用,则该样式具有不同的背景颜色

我该怎么做? 我已经创建了下面的代码来响应我自己介绍的变量,但是是否可以让它在按钮上进行响应。disabled()状态

我的代码:

struct MyButtonStyle: ButtonStyle {
    var enabledState = false

    func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
            .foregroundColor(Color.white)
            .padding(10)
            .padding(.horizontal, 20)
            .background(self.enabledState ? Color(UIColor.orange) : Color(UIColor.lightGray))
            .cornerRadius(20)
            .frame(minWidth: 112, idealWidth: 112, maxWidth: .infinity, minHeight: 40, idealHeight: 40, maxHeight: 40, alignment: .center)
            .scaleEffect(configuration.isPressed ? 0.9 : 1.0)
    }
}

struct ContentView: View {
    @State private var buttonEnabled = false

    var body: some View {
        HStack {
            Button("Button") {
                self.buttonEnabled.toggle()
                print("Button pressed")
            }
        }
        .buttonStyle(MyButtonStyle(enabledState: self.buttonEnabled))
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

要跟踪
.disabled
,存在显示此状态的
环境值.isEnabled
。但是,环境值仅适用于视图,不适用于样式

因此,解决方案是创建跟踪isEnabled的自定义按钮,并将其传递到自己的样式中

下面是解决方案方法的演示(
MyButtonStyle
未更改)。用Xcode 12b测试

struct MyButton: View {

    let title: String
    let action: () -> ()

    @Environment(\.isEnabled) var isEnabled // to handle own state !!

    init(_ title: String, action: @escaping () -> ()) {
        self.title = title
        self.action = action
    }

    var body: some View {
        Button(title, action: action)
            .buttonStyle(MyButtonStyle(enabledState: isEnabled))
    }
}

struct ContentView: View {
    @State private var buttonEnabled = true

    var body: some View {
        HStack {
            MyButton("Button") {            // << here !!
                self.buttonEnabled.toggle()
                print("Button pressed")
            }
            .disabled(!buttonEnabled)       // << here !!
        }
    }
}
struct MyButton:视图{
标题:字符串
让动作:()->()
@环境(\.isEnabled)变量isEnabled//处理自己的状态!!
init(uu标题:字符串,操作:@escaping()->()){
self.title=标题
行动
}
var body:一些观点{
按钮(标题、操作:操作)
.buttonStyle(MyButtonStyle(启用状态:isEnabled))
}
}
结构ContentView:View{
@State private var buttonEnabled=true
var body:一些观点{
HStack{

MyButton(“Button”){/再次使用
环境。我启用了
,但这次使用了ViewModifier。这样做的好处是,您可以在其他视图上使用它,而不仅仅是按钮。此实现降低了按钮背景色的不透明度,因此无需注入新样式

struct MyButtonModifier: ViewModifier {
    @Environment(\.isEnabled) var isEnabled
    let backgroundColor: Color

    func body(content: Content) -> some View {
        content
            .background(backgroundColor.opacity(isEnabled ? 1 : 0.5))
    }
}
然后在代码中使用它作为

Button("foo") {
    // action
}
.modifier(MyButtonModifier(backgroundColor: Color.red))