Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 14, Swift UI 2 change background color of selected List item inside a NavigationLink

How can I change the background color of a list item while it is selected inside of a NavigationLink?

It is always highlighted in blue (see the attached screen). I tried to change the listRowBackgroundColor view modifier, but it didn't work.

NavigationView {
    
    List (templateModel.model.items) { item in
        
        NavigationLink(destination: DetailView(selectedCategory: item.name, dismiss: dismiss)
            .environmentObject(OrientationInfo())) {
             Text("Test")
                 .listRowBackground(Color.gray)
        }
        .background(Color.black)
        .buttonStyle(PlainButtonStyle())
        .listRowBackground(Color.gray)
    }    
    .background(Color.black) 
    .listStyle(InsetListStyle())
}

enter image description here

like image 548
Niels Avatar asked Nov 07 '22 05:11

Niels


1 Answers

Just use .accentColor, as below

enter image description here

List (templateModel.model.items) { item in

    // ... other content here

}
.accentColor(.gray)       // << here !!
like image 130
Asperi Avatar answered Nov 15 '22 04:11

Asperi