简体   繁体   中英

How to change Background Color of a View containing a List back to White in SwiftUI

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.

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))
                }
    
            }
    
            
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM