How to change SwiftUI list background color
source link: https://sarunw.com/posts/swiftui-list-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.
How to change SwiftUI list background color
Table of Contents
In iOS 16, we finally got a native way to change the background color of a list view in SwiftUI.
We can do this with the new view modifier, .scrollContentBackground
.
You can customize a list view background in two ways.
You can easily support sarunw.com by checking out this sponsor.
Sponsor sarunw.com and reach thousands of iOS developers.How to change SwiftUI list background color
To change the background color of a list view, you specified ShapeStyle
to .scrollContentBackground
, Color
is one of the shape styles we can use.
In this example, I set list view background color to pink.
List {
Section {
Text("Item 1")
}
Section {
Text("Item 2")
Text("Item 3")
Text("Item 4")
}
Section {
Text("Item 5")
Text("Item 6")
Text("Item 7")
}
}
.scrollContentBackground(Color.pink)
Set list view background color to pink.In the current beta, we can't use other ShapeStyle
like LinearGradient
.
I have filed a bug report and will update the post once I know it is the intended behavior or a bug.
You can easily support sarunw.com by checking out this sponsor.
Sponsor sarunw.com and reach thousands of iOS developers.How to hide SwiftUI list background
If you want to be creative with a background, you can choose to hide it using .scrollContentBackground(.hidden)
, and create you background in .background
modifier.
Here is an example where we use an image as a background.
List {
Section {
Text("Item 1")
}
Section {
Text("Item 2")
Text("Item 3")
Text("Item 4")
}
Section {
Text("Item 5")
Text("Item 6")
Text("Item 7")
}
}
.background {
Image("ventura")
}
.scrollContentBackground(.hidden)
Use image as a background.You may also like
Learn a dedicated modifier to change a list row background color in SwiftUI.
SwiftUI ListIn iOS 16, we can create a multiple text field with new initializers and a little help from the .lineLimit(_:) modifier.
SwiftUI iOS 16Part 2 in the series "Building Lists and Navigation in SwiftUI". We will explore a ScrollView, UIScrollView equivalent in SwiftUI.
SwiftUI ListRead more article about SwiftUI, List, iOS 16,
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.
New if let shorthand for optional binding
Swift 5.7 introduced a new syntax for optional binding. Let's see what it is and how it can improve our code.
Better way to get paths to system directories in iOS 16
In iOS 16, the URL got a whole pack of type properties that reference a different path within a user domain, e.g., URL.documentsDirectory.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK