SwiftUI List: How to Set background. This simple task can’t be achieved without this API.
After using React, Flutter, and SwiftUI, Thin SwiftUI is the most intuitive. Probably because it came out later. However, it is frustrating because there are often cases where you can’t control it at will like List Background.
Even this simple task can really annoy you.
What is the problem?
What do you expect? The result is not the way you expect.
Oh I don’t want this!
How About this?
It doesn’t fill the entire row!
You must use specific API
Use listRowBackground
modifier. This is the solution. Let me check.
Yeah!!! I wanted to set the entire row background to red!
How about List Background?
Now you have achieved Red Background on individual row. But how do I change the background color of List?
Nothing happens! How do I change the background of List?
Change List Background Color like this
Use UITableView.appearance()
in initializer.
It works!
Using LazyVStack with ScrollView
This is how you achieve the same thing using another views.
Entire Code
Conclusion
background
modifier is useless when applied to List.- Use
.listRowBackground()
modifier to change the background of each row. - Use
UITableView.appearance()
ininitializer
to change List Background. - You can use alternatively ScrollView with LazyVStack