When using an image in a List item, you sometimes want to tint that image, but only if the item isn’t selected. When it’s selected, you usually want the contents of the list item to be all-white, for contrast.
The backgroundProminence Environment value ostensibly exists for this purpose, but in my tests, it never seems to change. Am I doing something wrong? Is there an alternative solution?
For instance, this code:
import SwiftUI
struct ProminentBackgroundInList: View {
var body: some View {
List(selection: .constant(0)) {
ListItem().tag(0)
ListItem().tag(1)
}
}
}
struct ListItem: View {
@Environment(\.backgroundProminence) var backgroundProminence
var body: some View {
HStack {
Image(systemName: "person.fill")
.foregroundStyle(backgroundProminence == .standard ? .orange : .primary)
Text("Person")
}
}
}
#Preview {
ProminentBackgroundInList()
}
Produces this result: