44

How to change SwiftUI list row background color

 2 years ago
source link: https://sarunw.com/posts/swiftui-list-row-background-color/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

emerge-avatar-rounded-small.png

How to change SwiftUI list row background color

06 Jun 2022 ⋅ 1 min read ⋅ SwiftUI List

Table of Contents

You can easily support sarunw.com by checking out this sponsor.

emerge-avatar-rounded-small.png Sponsor sarunw.com and reach thousands of iOS developers.

How to change SwiftUI list row background color

We can change the background color of a list row in SwiftUI with listRowBackground(_:) modifier.

To set a list row background color, add listRowBackground(_:) modifier to the list row item.

You can use listRowBackground(_:) in two places:

  1. On a List row item.
  2. On a ForEach that populates list row item.

It is tempting to use listRowBackground(_:) on a list view, but that won't work.

struct ContentView: View {
var body: some View {
List {
ForEach((1...100), id: \.self) {
Text("Row \($0)")
.listRowBackground(Color.pink)
}
// This also work
// .listRowBackground(Color.pink)
}
// This won't work
// .listRowBackground(Color.pink)
}
}
.listRowBackground(Color.pink)
.listRowBackground(Color.pink)

One thing to note here is a list row item appearance varies based on the list style. It might not look good on all of them.

Here are what it look like on .plain, .inset, .grouped, .insetGrouped, and .sidebar list style.

Pink list row background color on .plain, .inset, .grouped, .insetGrouped, and .sidebar list style.
Pink list row background color on .plain, .inset, .grouped, .insetGrouped, and .sidebar list style.

You may also like

List view, a UITableView equivalent in SwiftUI

Part 3 in the series "Building Lists and Navigation in SwiftUI". We will explore a List, UITableView equivalent in SwiftUI.

SwiftUI List
How to add section header and footer to SwiftUI list

We can group related data in a SwiftUI list using Section view. We can also optionally add a header and footer to describe a particular section. Let's learn how to do that.

SwiftUI List
How to use ScrollView in SwiftUI

Part 2 in the series "Building Lists and Navigation in SwiftUI". We will explore a ScrollView, UIScrollView equivalent in SwiftUI.

SwiftUI List

Read more article about SwiftUI, List,

or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron

Buy me a coffee

Tweet

Share

Previous
CollectionView in SwiftUI with LazyVGrid and LazyHGrid

Learn the difference between a horizontal and vertical grid and everything you need to know to use them.

Next
First impressions of SwiftUI in WWDC22

An overview of what's new in SwiftUI.

← Home


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK