Search code examples
listswiftuicolorsnavigationview

How to add background Color to a navigationView in swiftUI


I am trying to add a background color to the navigation view but it doesn't apply to the whole view.

var body: some View {
    NavigationView {
        VStack(spacing: 20) {
            TextField("Add text here...", text: $textFieldText)
                .font(.headline)
                .padding(.leading)
                .frame(height: 55)
                .background(back)
                .cornerRadius(10)
                .padding(.horizontal)
            
            Button(action: {
                guard !textFieldText.isEmpty else { return }
                vm.addFruit(text: textFieldText)
                textFieldText = ""
            }, label: {
                Text("Save")
                    .font(.headline)
                    .frame(maxWidth: .infinity)
                    .frame(height: 55)
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(10)
                    .padding(.horizontal)
            })
            
            List {
                ForEach(vm.savedEntities) { entity in
                    Text(entity.name ?? "NO NAME")
                        .onTapGesture {
                            vm.updateFruit(entity: entity)
                        }
                }
                .onDelete(perform: vm.deleteFruit)
            }
            .onAppear() {
                UITableView.appearance().backgroundColor = UIColor.clear
            }
        }
        .navigationTitle("Fruits")
        .background(Color.red.ignoresSafeArea(.all))
    }
    .navigationViewStyle(StackNavigationViewStyle())
}

}

I have tried several things but none seem to work. Is this a problem for everyone using xcode 14? Also how do I reslove it?


Solution

  • ...but it doesn't apply to the whole view., it could be due to the List scroll background, in that case try this:

     List {
        ...
      }
      .scrollContentBackground(.hidden)
      .background(Color.red)