1

I am currently working with Lists in SwiftUI.

Problem: A default View in SwiftUI has a white background Color. However, when adding a List to it, the Background Color changes to systemGray6 while the List Cells take the white bgColor.

Goal: I would like to have a List with white bgColor and Cells with systemGray6 bgcolor.

I accomplished to change the Background Color of the List Rows :

List(users) { user in
    // ...
}
.listRowBackground(Color(UIColor.systemGray6)

However, I can't find a solution for changing the Screen's bgColor back to white. I tried the obvious stuff like .background(Color.white).

Does anyone has a solution for this issue?

Thanks for your help.

christophriepe
  • 1,157
  • 12
  • 47
  • 1
    Does this answer your question? [SwiftUI List color background](https://stackoverflow.com/questions/57128547/swiftui-list-color-background) – Witek Bobrowski Nov 27 '20 at 11:54

1 Answers1

2

you can change those colors as you wants, here a show example:(tested with Xcode 12.1 / iOS 14.1)

struct ContentView: View {
        
        init() {
            
            UITableView.appearance().backgroundColor = UIColor.white
        }
        
        var body: some View {
    
            
            List
            {
      
                ForEach(0 ..< 20) { index in
                    Text("Row \(index)")
                        .listRowBackground(Color(UIColor.systemGray6))
                }
    
            }
    
            
        }
    }
ios coder
  • 1
  • 4
  • 31
  • 91
  • thanks for your help. This does work and solve my issue. However, is there no more-`SwiftUI`-like way to do this? – christophriepe Nov 27 '20 at 12:21
  • your are very welcome, I had the same question like you have, you have to build your own Custom List, it need's more coding and much more coding for scrolling or delete row. but it is possible, again need's lot's coding for building custom List. Basically you would start with zero. But if you want just simple and shortcut use that code and List, right now SwiftUI is just developing, it is not developed like UIKit and mostly of SwiftUI codes are using UIKIt codes under-hod. I would say wait for higher version of SwiftUI or build your own ListView. I already build my own. – ios coder Nov 27 '20 at 12:39
  • Almost I forgot! you can use ForEach with ScrollView as well to getting kinda List view as well. that is also an option. – ios coder Nov 27 '20 at 12:44
  • Yes that's true. However the whole editing and deleting functionality is not included. I guess the best solution for me is just changing the bgColor like you explained. – christophriepe Nov 27 '20 at 12:48
  • yes, that is best option for you otherwise you would lose lot's of your time on building the custom List, which maybe Apple push a new update for SwiftUI and fix that issue as well also. – ios coder Nov 27 '20 at 13:04