我想要实现的是具有不同背景颜色的表单。但我希望里面的项目始终使用浅色模式变体(深色文本颜色)。但不是完整的 View 。我已经搜索过但找不到任何解决方案。
我尝试使用 listRowBackground
更改有效项目的背景,并尝试 preferredColorScheme
使用浅色模式,但这将使完整 View 处于浅色模式:
...
Form {
DatePicker(selection: $date, displayedComponents: .date) { ... }
.listRowBackground(Color.yellow)
TextField("Name", text: $name)
.listRowBackground(Color.yellow)
Picker("amount", selection: $amount) {
ForEach(amounts, id: \.self) {
Text($0)
}
}
.listRowBackground(Color.yellow)
Button(action: { ... }) {
Label("More", systemImage: "chevron.up")
}
.listRowBackground(Color.yellow)
}
.preferredColorScheme(.light)
...
最佳答案
您可以使用.environment
强制 View 的配色方案:
.environment(\.colorScheme, .light)
如果您不想对整个表单执行此操作,则可以仅将其注入(inject)到嵌套 View 中:
Form {
Group {
DatePicker(selection: $date, displayedComponents: .date) { ... }
.listRowBackground(Color.yellow)
TextField("Name", text: $name)
.listRowBackground(Color.yellow)
Picker("amount", selection: $amount) {
ForEach(amounts, id: \.self) {
Text($0)
}
}
.listRowBackground(Color.yellow)
Button(action: { ... }) {
Label("More", systemImage: "chevron.up")
}
.listRowBackground(Color.yellow)
}
.environment(\.colorScheme, .light)
}
(或仅单个 View )
Form {
// ...
TextField("Name", text: $name)
.listRowBackground(Color.yellow)
.environment(\.colorScheme, .light)
// ...
}
关于ios - 是否可以仅覆盖表单的暗模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66695827/