Updated for Xcode 14.2
SwiftUI has a dedicated modifier for setting background views behind list rows, called listRowBackground()
. This accepts any kind of view – including colors, images, and shapes – and uses it behind rows.
For example, this creates a list using 10 rows, each with a red background color:
List {
ForEach(0..<10) {
Text("Row \($0)")
}
.listRowBackground(Color.red)
}
Download this as an Xcode project
Tip: listRowBackground()
won’t affect any parts of the list that don’t contain data.
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.