Does anyone know how to achieve this? I have four colors of red, green, yellow, and blue and I want my selected segment to have a background/tint color that is indicative of the color name.
This is my code:
let objectColors = Color.allCases
@State private var selectedColorIndex = 0
//declared inside body view
Picker("Colors", selection: $selectedColorIndex){
ForEach(0..<objectColors.count){ index in
Text(self.objectColors[index].rawValue).tag(index)
}
}.pickerStyle(SegmentedPickerStyle())
.padding(10)
.onAppear {
UISegmentedControl.appearance().selectedSegmentTintColor = UIColor.generateUIColor(colorIndex: selectedColorIndex)
}
This is the list I'm pulling from
enum Color: String, CaseIterable{
case red, yellow, green, blue
}
I've tried using onChange or onReceive (and Combine's 'Just()' for subview) instead of onAppear but they crash on playgrounds and don't work on Xcode.
I also saw a WWDC video on UIAction that I think will work great for updating view changes but I have no idea how to translate it. Does anyone one have any suggestions or help, please? Thanks