在 SwiftUI 中更改按钮背景颜色的最佳方法是什么?
我想创建一个图块,它是一个带有居中文本和某种颜色的矩形背景的按钮。
如果您使用 .buttonStyle(style:) 修饰符并想要更改背景颜色,请使用 .tint(tint:) 而不是 .background(alignment:content)。例如,您可以通过以下方式将 .borderedProminent 按钮样式更改为绿色:
Button("Press Me!") {
//stuff to do
}
.buttonStyle(.borderedProminent)
.tint(.green)
Button(action: {self.buttonTapped()}) {
Text("Button")
.padding(.all, 12)
.foregroundColor(.white)
.background(Color.red)
}
这就是整个背景可点击的方式
SwiftUI 颜色
从 iOS v13 及更高版本开始,当您使用颜色时,应考虑浅色和深色模式。
.foregroundColor(.red)
.background(.blue)
例如,深色模式下的 .black 将不可见(黑底黑字)。您可以使用系统颜色作为变体。例如
UIColor.label