0

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

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

8 Comments

This works, and thanks. The "UITableView.appearance().backgroundColor = .yellow" code I will have to customize but that shouldn't be a problem.
It shouldn’t be very different from what you did, it’s straight forward, if you have any troubles please let me know.
Great! Good luck.
what if I want to set an image to a background? @Muhand Jumah
@MertKöksal you could set color to clear and put your Image and List in a Zstack
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.