Thank you for your reply! I understand now, and was able to fix our code.
I’d read the documentation page too quickly and misunderstood what it was saying exactly. It's pretty interesting that you need to read the environment not from within the row view, but from a descendant of it.
For anyone else reading this, here’s some additional tests that helped me understand exactly the required setup. The third row especially (ListItemThinWrapper) seems to suggest you should be thinking in terms of resolved views, or something like that.
struct ProminentBackgroundInList: View {
var body: some View {
List(selection: .constant(Set([0, 1, 2, 3]))) {
ListItem().tag(0) // doesn't work
ZStack {
ListItem() // works
}.tag(1)
ListItemThinWrapper().tag(2) // doesn't work
ListItemStackWrapper().tag(3) // works
}
}
struct ListItem: View {
@Environment(\.backgroundProminence) var backgroundProminence
var body: some View {
HStack {
Image(systemName: "person.fill")
.foregroundStyle(backgroundProminence == .standard ? .orange : .primary)
Text("Person")
}
}
}
struct ListItemThinWrapper: View {
var body: some View {
ListItem()
}
}
struct ListItemStackWrapper: View {
var body: some View {
ZStack {
ListItem()
}
}
}
}
#Preview {
ProminentBackgroundInList()
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: