Updated for Xcode 14.2
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)
}
Download this as an Xcode project
Like I said, exactly what that does depends on the platform:
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))
}
Download this as an Xcode project
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.
SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
Link copied to your pasteboard.