所以我在操場上運行下面的代碼,我看到 2 個關于亮外觀的按鈕,然后我切換到暗模式。作為 SwiftUI 框架的一部分,這兩個按鈕都將前景色更新為灰白色。然后我點擊一個按鈕,其他按鈕上的前景色變為灰色。見下面附上的視頻。使用 Xcode 13.1
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
var body: some View {
VStack {
HStack {
Text("Current")
.font(.subheadline)
.fontWeight(.semibold)
.foregroundColor(.gray)
.frame(maxWidth: .infinity, alignment: .leading)
Label("43.672490, -79.388932", systemImage: "location")
.frame(width: 175, alignment: .trailing)
}
HStack {
Button {} label: {
Text("?? Set Location")
.frame(width: 95)
}
Spacer()
Button {} label: {
Text("?? Set Travel")
.frame(width: 95)
}
}
}
}
}
PlaygroundPage.current.setLiveView(ContentView())
外觀變更按鈕問題視頻
任何人都遇到過這個錯誤并有解決方案,還是我必須創建一個自定義按鈕并手動修改前景色?
uj5u.com熱心網友回復:
前景色確實似乎沒有在明暗模式之間正確調整。但是,我能夠使用一種顏色來修復它,該顏色通過.foregroundColor()在按鈕上將其添加為 a 來考慮不同的外觀:
struct ContentView: View {
let buttonTextColor = Color(.secondaryLabelColor)
var body: some View {
VStack {
HStack {
Text("Current")
.font(.subheadline)
.fontWeight(.semibold)
.foregroundColor(.gray)
.frame(maxWidth: .infinity, alignment: .leading)
Label("43.672490, -79.388932", systemImage: "location")
.frame(width: 175, alignment: .trailing)
}
HStack {
Button {} label: {
Text("?? Set Location")
.frame(width: 95)
}
.foregroundColor(buttonTextColor)
Spacer()
Button {} label: {
Text("?? Set Travel")
.frame(width: 95)
}
.foregroundColor(buttonTextColor)
}
}
}
}
恕我直言,值得向 Apple 報告錯誤。
編輯:
要完全模仿按鈕,您需要使用自定義ButtonStyle. 我沒有完全匹配顏色,因為你可以自己做,但這里是它的作業原理:
struct ContentView: View {
@Environment(\.colorScheme) var colorScheme
var buttonTextColor = Color(.labelColor)
var body: some View {
VStack {
HStack {
Text("Current")
.font(.subheadline)
.fontWeight(.semibold)
.foregroundColor(.gray)
.frame(maxWidth: .infinity, alignment: .leading)
Label("43.672490, -79.388932", systemImage: "location")
.frame(width: 175, alignment: .trailing)
}
HStack {
Button {} label: {
Text("?? Set Location")
.frame(width: 95)
}
.buttonStyle(CustomButtonStyle())
Spacer()
Button {} label: {
Text("?? Set Travel")
.frame(width: 95)
}
}
}
}
}
struct CustomButtonStyle : ButtonStyle {
@Environment(\.colorScheme) var colorScheme
func makeBody(configuration: Configuration) -> some View {
if colorScheme == .light {
configuration.label
.foregroundColor(configuration.isPressed ? Color.white : Color.black)
.padding(3)
.background(RoundedRectangle(cornerRadius: 5)
.fill(configuration.isPressed ? Color.blue : Color.white)
.shadow(color: .gray, radius: 2, x: 0, y: 2)
)
} else {
configuration.label
.foregroundColor(Color.white)
.padding(3)
.background(RoundedRectangle(cornerRadius: 5)
.fill(configuration.isPressed ? Color.blue : Color.gray)
.shadow(color: .gray, radius: 2, x: 0, y: 2)
)
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/365647.html
