我正在尝试在SwiftUI中更改Button
的颜色。这是我的整个CustomButton视图结构:
我试图在SwiftUI中更改Button
的颜色。这是我的整个CustomButton视图结构:
struct CustomButton: View {
@State private var didTap:Bool = false
var body: some View {
Button(action: {
self.didTap = true
}) {
Text("My custom button")
.font(.system(size: 24))
}
.frame(width: 300, height: 75, alignment: .center)
.padding(.all, 20)
.background(Color.yellow)
//My code above this comment works fine
//I tried something like this, but it is not working
// if didTap == true {
// .background(Color.blue)
// }
}
}
这是我的按钮外观(很好):
但我的问题是:当用户点击此按钮时,如何更改背景颜色。
谢谢。
self.didTap.toggle()
替代self.didTap = true
,这样每次点击后它就会来回切换。@pizzae - jwknz