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.
SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.