你能在运行时访问Picker元素并更改它们的颜色吗



我想更改Picker的当前选定元素的颜色。这可能吗?这是我的密码。

Picker("Select a Project", selection: $selectedProjectString){
ForEach(projects, id: .self) {
Text($0).lineLimit(1).frame(height:120)
}
}.foregroundColor(Color.white)
.frame(height: 120)

selectedProjectString匹配的元素应该是与所有其他元素不同的颜色。或者可以访问特定的元素,比如使用索引之类的吗?

我想出了这个解决方案:

Picker("Select a Project", selection: $selectedProjectString){
ForEach(projects, id: .self) {
if (startedProject == $0) {
Text($0).lineLimit(1).frame(height:120).foregroundColor(Color.red)
} else {
Text($0).lineLimit(1).frame(height:120)
}

}
}.foregroundColor(Color.white)
.frame(height: 120)

我检查所选元素是否等于某个值,如果,我更改它所属的Text对象的样式。因为我更改了变量的值,所以视图会被重新加载,并刷新我的选择器。现在,选定的元素具有颜色。

最新更新