Mattis Schulte
Mattis Schulte

Reputation: 699

How can I change the background color in SwiftUI when I am using NavigationView and List?

I would like to change my background color, but I am using NavigationView and List. That's why the ZStack method doesn't work for me.

This is my Code:

NavigationView {
    List {
        Text("Some Text")
    }

    .navigationBarTitle("Test")
}

Upvotes: 0

Views: 342

Answers (3)

Mattis Schulte
Mattis Schulte

Reputation: 699

Finally I have found an answer to my problem:

init() {
    UITableView.appearance().backgroundColor = UIColor(named: "CustomBackgroundColor")
    UITableViewCell.appearance().backgroundColor = UIColor(named: "CustomBackgroundColor")
    UITableView.appearance().tableFooterView = UIView()
}

var body: some View {
    NavigationView {
        List {
            Text("Hello")
        }

        .navigationBarTitle("Test")
    }
}

Upvotes: 0

E.Coms
E.Coms

Reputation: 11531

You can use .listRowBackground inside the List.

  ZStack{
        Color.blue
   NavigationView {
    ZStack{
        Color.red
       List {
        ZStack{
            Color.green
            Text("Some Text").background(Color.green).opacity(0.5)

        }.listRowBackground(Color.red)
        }
    }
       .navigationBarTitle("Test")
   }.padding()

    }

Upvotes: 1

Dmytro Antonchenko
Dmytro Antonchenko

Reputation: 326

NavigationView {
    List {
         Text("Some Text")
    }
    .navigationBarTitle("Test")
}.colorMultiply(Color.green)

Upvotes: 0

Related Questions