I want to set the selected segment color in a SwiftUI segmented picker and change the text color to white.
I have tried both using the modifiers for the picker view and modifying the tint color from the appearance proxy. None of them seem to work, unfortunately.
import SwiftUI
struct PickerView: View {
@State var pickerSelection = 0
init() {
UISegmentedControl.appearance().tintColor = UIColor.blue
}
var body: some View {
Picker(selection: $pickerSelection, label: Text("")) {
Text("Active").tag(0).foregroundColor(Color.white)
Text("Completed").tag(1)
}.pickerStyle(SegmentedPickerStyle()).foregroundColor(Color.orange)
}
}
Is there any way to do this in SwiftUI, or should I just use the UISegmentedControl by using UIViewControllerRepresentable?
Best Solution
Native ( but limited )
SwiftUI is not currently supporting native SegmentedPicker styling (see the bottom of the answer for the working workaround). But there is a limited way to apply a color to the segmented picker using
.colorMultiply()
modifier:Full control using
UIAppearance
selectedSegmentTintColor
is available since beta 3 for changing the color of the selected segment.For changing the
textColor
, you should usesetTitleTextAttributes
for.selected
state and.normal
state (unselected).So it will be like:
Also as mike mentioned, you can set the background color too like:
Also, don't forget you can set SwiftUI colors too! For example: