NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

HELP: Segmented Picker Tint Color

Forums > SwiftUI

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
    .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


The way I have done it is

enum Colors: String, CaseIterable{
    case red, yellow, green, blue

    func displayColor() -> String {
struct ContentView: View {
    @State private var selectedColor =

    var body: some View {
        Picker(selection: $selectedColor, label: Text("Colors")) {
            ForEach(Colors.allCases, id: \.self) { color in

    func color(_ selected: Colors) -> Color {
        switch selected {
        case .red:
            return .red
        case .yellow:
            return .yellow
        case .green:
            return .green
        case .blue:
            return .blue


Hacking with Swift is sponsored by Instabug

SPONSORED Catch bugs as soon as they happen and know exactly why a crash occurred. Instabug's SDK grabs all the logs they need to fix bugs, crashes and performance issues in minutes instead of days. Get screenshots, device details, network logs, repro steps, and tons of other critical insights needed to resolve issues and prioritize product backlogs straight from your dashboard. It only takes a minute to integrate!

Get started now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.