WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

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.

Save 50% in my WWDC23 sale.

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.

Save 50% on all our books and bundles!

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!

Average rating: 5.0/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.