2

I have a SwiftUI List that has its listStyle set to SidebarListStyle. This gives me a List that looks like this:

enter image description here

Great! However, I would like to set a different background color on it, and can't seem to figure out how to do it. My code looks like this:

List {
   <snip>
}
.listStyle(SidebarListStyle())
.background(Color.yellow.ignoresSafeArea())

But, this effectively does nothing. My List looks exactly the same. How can I go about setting the background color for this List?

Shadowman
  • 11,150
  • 19
  • 100
  • 198

3 Answers3

1

You can use this modifier .listRowBackground() for background color of a separate row.


struct ContentView: View {
    
    private var items = ["1", "2", "3"]
    
    init(){
        UITableView.appearance().backgroundColor = .purple // background color of list
    }
    
    var body: some View {
        List {
            ForEach(items, id: \.self) { item in
                Text(item)
                    .listRowBackground(Color.yellow) // background color of listRow
                Text(item)
                    .listRowBackground(Color.blue) // background color of listRow
            }
        }
        .frame(width: 300, height: 600)
    }
}



enter image description here

Taeeun Kim
  • 964
  • 1
  • 8
  • 20
  • That works fine for the rows themselves. What about the background of the entire List? It's still showing as gray. – Shadowman Jul 21 '21 at 16:39
  • @Shadowman you can use `UITableView.appearance().backgroundColor` for background color of list I updated my post for that :) – Taeeun Kim Jul 21 '21 at 16:46
  • Excellent! Is there an equivalent for macOS apps that can't make use of UIKit? – Shadowman Jul 21 '21 at 16:49
  • Also, the UITableView.appearance() approach changes *every* List background color. Is there no way to change them on an individual basis? – Shadowman Jul 21 '21 at 16:55
  • @Shadowman https://stackoverflow.com/questions/60454752/swiftui-background-color-of-list-mac-os would be a answer to your first ask about using in macOS. For the second ask, you can solve it by creating a struct for each list separately. I am not sure, if there is a way now without `init(){UITableView.appearance()}` for list background color with SwiftUI in iOS – Taeeun Kim Jul 21 '21 at 17:01
  • @Shadowman If my answer was helpful, you can accept my answer :D – Taeeun Kim Jul 22 '21 at 16:13
  • Hi How do I change the disclosure indicator color – Kishorekumar E K Nov 18 '21 at 11:25
  • this finally has an answer, see my post – Peter Lapisu Nov 10 '22 at 10:59
0

This is the holy grail as of iOS 16+ and macOS 13+

.listStyle(SidebarListStyle())
.background(Color.red)
.scrollContentBackground(.hidden)
Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179
0
.scrollContentBackground(.hidden)
.background(.red.opacity(0.2))

Add this to your form, that's it!

  • As it is currently written, your answer is unclear. Please [Edit](https://stackoverflow.com/posts/75764050/edit) to explain what your code does or add other supporting information. –  Mar 22 '23 at 21:58
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 23 '23 at 07:39