1

I have this code . I want to change the background color of the row with the name "Aditya" in the EditForm View within the Content View background color to , for example gray . What i am doing now is setting the color after the list but it does not do what i want which is setting just the row with "Aditya"name background color to gray. How can i do that within the content view .

  struct ContentView: View {
        
        var someelements = ["Aditya" , "Kappor" , "Chattarjee", "Mithun"]
        var body: some View {
                List {
                 ForEach (self.someelements , id: \.self ) { value in
                    EditForm(name: value , action: {
                        if value == "Aditya" {
                            /*what i am doing*/
                            self.listRowBackground(Color(.gray))
                        }
                    })
                }
                }
            }
    }
    
    
    
    struct EditForm : View {
        var name : String
        var action: (()->())?
        
        var body: some View {
            Group {
                Button (action: {
                    //some action
                }) {
                    HStack {
                        Text(self.name)
                            .scaledToFit()
                            .foregroundColor(.red)
                        
                    }
                }
            }
        }
    
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }


  
  • You want to remove the background and set a own color.. see this question. It works for iOS aswell https://stackoverflow.com/questions/60454752/swiftui-background-color-of-list-mac-os – davidev Dec 08 '20 at 22:08

1 Answers1

1

Here is a demo of solution. Prepared with Xcode 12.1 / iOS 14.1

demo

struct ContentView: View {
    
    var someelements = ["Aditya" , "Kappor" , "Chattarjee", "Mithun"]
    
    var body: some View {
        List {
            ForEach (self.someelements , id: \.self ) { value in
                EditForm(name: value , action: {
                    
                }).listRowBackground(value == "Aditya" ? Color.gray : Color.clear)
            }
        }
    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690