listbackgroundswiftuizstack

Having Problems Setting View Background Color with List View--SwiftUI


enter image description hereI want to set the View Background color to green. I have a List View and it's fine but the underlying view needs to be green--see image. I know I'm missing something but with SwiftUI you sometimes don't know if it's a bug or just how the thing is supposed to work. Any ideas that have worked for others I would appreciate. I have attached my code and I am on Xcode Version 11.5 (11E608c).

Here is the code:

var body: some View {
     ZStack() {
          Color(red: 95.0/255.0, green: 122.0/255.0, blue: 128.0/255)
               .edgesIgnoringSafeArea(.all)

          Text("")
          List {
              Group {
                  Text("Price: ")
                  Text("Down: ")
                  Text("APR: ")
                  Text("Term: ")
                  Text("Tax: ")
                  Text("Rebate: ")
              }.listRowBackground(Color(red: 95.0/255.0, green: 122.0/255.0, blue: 128.0/255))

              Group {
                  Text("Add On: ")
                  Text("Fees: ")
                  Text("Trade Value: ")
                  Text("Total Interest: ")
                  Text("Payment: ")
              }.listRowBackground(Color(red: 95.0/255.0, green: 122.0/255.0, blue: 128.0/255))
          }
      }
   }

Solution

  • Unfortunately swiftui doesn't support this yet, however, List is just a uitableview under the hood so you can just change the UITableView properties.

    struct StackOverflow1: View {
       // you need to modify the UItableview in the initializer
       init() {
            UITableView.appearance().backgroundColor = .yellow       // Change background color
            UITableViewCell.appearance().backgroundColor = .green    // Change each cell's background color
       }
    
        var body: some View {
           // your code goes here
        }
    }
    

    Its important to note that this will change all List instances in this view so if you have multiple Lists they all will be the same colors.

    to overcome this issue you can make different views for each List and then embed them in your main view.