How to Change Picker Text Color In SwiftUI

I try to change picker text color but, it does not work.

As you see, "City" and "District"'s color are blue but, I'd like to make them white. I have tried below codes but, they do not work.

Do you know any methods for that ?

Picker("İl", selection: $selectedCity) {
      ForEach(turkishCities, id: \.self) { city in
         Text(city)
           .foregroundColor(.white)
      }
}
Picker("İlçe", selection: $selectedDistrict) {
    ForEach(cityDistricts[selectedCity] ?? [], id: \.self) { district in
        Text(district)
            .foregroundColor(Color.white)
    }
}
.onAppear {
UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.white], for: .normal)
}

Thank you in advance.

Accepted Reply

Using accentColor should do it:

Picker("İl", selection: $selectedCity) {
      ForEach(turkishCities, id: \.self) { city in
         Text(city)
      }
} 
 .accentColor(.red)

A small example to show to select between User1, User2, User3:

            Picker("User", selection: $selectedUser) {
                ForEach(users, id: \.self) { user in
                    Text(user)
                        .tag(user)
                }
            }
            .accentColor(.red)

With accentColor:

without accentColor:

Note: that does not work If the picker is within a form.

  • @Claude31 Thank you. It works.

  • I was looking for the same question and benefited from this answer, thank you! However, Xcode warns that the accentColor modifier will be deprecated. As it suggests, the tint modifier also changes the color in the desired manner.

Add a Comment

Replies

Using accentColor should do it:

Picker("İl", selection: $selectedCity) {
      ForEach(turkishCities, id: \.self) { city in
         Text(city)
      }
} 
 .accentColor(.red)

A small example to show to select between User1, User2, User3:

            Picker("User", selection: $selectedUser) {
                ForEach(users, id: \.self) { user in
                    Text(user)
                        .tag(user)
                }
            }
            .accentColor(.red)

With accentColor:

without accentColor:

Note: that does not work If the picker is within a form.

  • @Claude31 Thank you. It works.

  • I was looking for the same question and benefited from this answer, thank you! However, Xcode warns that the accentColor modifier will be deprecated. As it suggests, the tint modifier also changes the color in the desired manner.

Add a Comment

As @obaytimur proposed, tint seems the way to go. Also, in some cases accentColor doesn't seem to work. I had a quite nested view with the picker inside, and the text color wouldn't change with accentColor. tint worked. Thanks