How to change the tint color for individual list rows
About 2 min
How to change the tint color for individual list rows 관련
SwiftUI by Example
Back to Home
How to change the tint color for individual list rows | SwiftUI by Example
How to change the tint color for individual list rows
Updated for Xcode 15
SwiftUI has a dedicated listItemTint()
modifier that controls how the list colors its rows. The exact behavior depends on which platform your app is running on, but the code is the same. For example, this will tint even rows red and odd rows green:
List(1..<51) { i in
Label("Row \(i)", systemImage: "\(i).circle")
.listItemTint(i.isMultiple(of: 2) ? .red : .green)
}
Like I said, exactly what that does depends on the platform:
- On iOS that will change the icons to be red and green, but leave the text in its primary color.
- On macOS that will also change the icons to be red and green, overriding the user's preferred accent color.
- On watchOS that will change the row background color (known as its “background platter”) to be red or green.
- On tvOS it will do nothing at all.
On macOS, you can respect the user's accent color while also adding your own preferred list row tint like this:
List(1..<51) { i in
Label("Row \(i)", systemImage: "\(i).circle")
.listItemTint(.preferred(i.isMultiple(of: 2) ? .red : .green))
}
That will now apply the user's selected accent color if they have one, but if they have the Multicolor accent set then the rows will be tinted red or green as before.
Similar solutions…
How to set the background color of list rows using listRowBackground() | SwiftUI by Example
How to set the background color of list rows using listRowBackground()
How to control spacing around individual views using padding | SwiftUI by Example
How to control spacing around individual views using padding
How to change the background color of List, TextEditor, and more | SwiftUI by Example
How to change the background color of List, TextEditor, and more
How to let users delete rows from a list | SwiftUI by Example
How to let users delete rows from a list
How to add a badge to TabView items and List rows | SwiftUI by Example
How to add a badge to TabView items and List rows