SwiftUI List: How to Set background. This simple task can’t be achieved without this API.

KD Knowledge Diet
2 min readMay 25, 2022

--

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?

I want red background!!

What do you expect? The result is not the way you expect.

Unexpected Behavior

Oh I don’t want this!

How About this?

Adding HStack with Spacer.. Would it work as I expect?
NO NO NO !!!

It doesn’t fill the entire row!

You must use specific API

Use listRowBackground modifier. This is the solution. Let me check.

Would this work?
Yeah!!!

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?

Would this work?
Nothing Happens

Nothing happens! How do I change the background of List?

Change List Background Color like this

Use UITableView.appearance() in initializer.

Would this work?
It works!

It works!

Using LazyVStack with ScrollView

This is how you achieve the same thing using another views.

LazyVStack with ScrollView

Entire Code

Entire Code

Conclusion

  • background modifier is useless when applied to List.
  • Use .listRowBackground() modifier to change the background of each row.
  • Use UITableView.appearance() in initializer to change List Background.
  • You can use alternatively ScrollView with LazyVStack

--

--

KD Knowledge Diet
KD Knowledge Diet

Written by KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!

No responses yet