HStack(spacing: 15) {
ForEach(0..<button.count, id: \.self) {button in
Button(action: {
self.buttonContinue = button
}) {
Text("\(self.button[button])").padding(.vertical, 12.5)
.padding(.horizontal, 120)
.foregroundColor(.white)
.background(self.buttonContinue == button ? Color.black: Color.gray)
.clipShape(Capsule())}}
}
我已經使用此代碼創建了一個繼續按鈕。在淺色模式下,顏色效果很好(從灰色背景和白色文本到黑色背景和白色文本),但是,當我切換到深色模式時,按鈕的背景會在單擊時從灰色消失。無論如何我可以在暗模式下將按鈕的背景更改為白色(因為我嘗試過只能更改文本顏色)?
uj5u.com熱心網友回復:
您可以在 Color(uiColor:) init 中使用 UIColor,也可以創建您自己的具有 Any Appearance(淺色)和深色模式顏色的顏色資源。使用 UIColor 如下:
Text("\(self.button[button])").padding(.vertical, 12.5)
.padding(.horizontal, 120)
.foregroundColor(.white)
// See below for the appropriate inits:
.background(self.buttonContinue == button ? Color(uiColor: .labelColor): Color(uiColor: .systemGrayColor)
.clipShape(Capsule())}}
或使用顏色資源:

uj5u.com熱心網友回復:
在你的情況下,你可以簡單地做:
Text("Button \(button)")
.padding(.vertical, 12.5)
.padding(.horizontal, 120)
.foregroundStyle(.background)
.background(2 == button ? Color.primary: Color.secondary)
.clipShape(Capsule())
或更多控制用途:
@Environment(\.colorScheme) var colorScheme
var textColor: Color {
if colorScheme == .dark {
return Color.white
} else {
return Color.black
}
}
或關注@loremipsum 并在 Assets 中定義您自己的顏色
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/447667.html
上一篇:需要想法如何決議以下JSON格式
