我在 SwiftUI 中有一个分段选择器。我希望选择器中的每个元素都有自己的颜色。
我试过这个:
struct ContentView: View {
@State private var selectedColorIndex = 0
var body: some View {
VStack {
Picker("Favorite Color", selection: $selectedColorIndex, content: {
Text("Red").foregroundColor(.red).tag(0)
Text("Green").tag(1).foregroundColor(.green)
Text("Blue").tag(2).foregroundColor(.blue)
})
.pickerStyle(.segmented)
Text("Selected color: \(selectedColorIndex)")
}
}
}
但是选择器中的项目不会改变颜色。我觉得这很奇怪,因为项目只是简单的文本视图.....