NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

How to change the tint color for individual list rows

Paul Hudson    @twostraws   

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 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))
}

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.

Hacking with Swift is sponsored by Play

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!

Click to learn more about Play!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.