ios – SwiftUI NavigationLink iOS16 selected tint?

0
62

[ad_1]

I have the following code:

struct ContentView: View {
    @Environment(\.managedObjectContext) private var viewContext

    @FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],
        animation: .default)
    private var items: FetchedResults<Item>
    
    @State private var selectedMainItem:Item?

    var body: some View {
        
        NavigationSplitView {
            
            List(selection: $selectedMainItem) {
                ForEach(items) { item in
                    NavigationLink(value: item) {
                        Text("Item  \(item.id.debugDescription)")
                    }
                }
                .onDelete(perform: deleteItems)
            }
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    EditButton()
                }
                ToolbarItem {
                    Button(action: addItem) {
                        Label("Add Item", systemImage: "plus")
                    }
                }
            }

            
        } detail: {
            NavigationStack {
                if let selectedMainItem = selectedMainItem {
                    Text("Item at \(selectedMainItem.timestamp!, formatter: itemFormatter)")
                } else {
                    Text("Please select a main item")
                }
            }
            
            
        }
    }

    private func addItem() {
        withAnimation {
            let newItem = Item(context: viewContext)
            newItem.timestamp = Date()

            do {
                try viewContext.save()
            } catch {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                let nsError = error as NSError
                fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
            }
        }
    }

    private func deleteItems(offsets: IndexSet) {
        withAnimation {
            offsets.map { items[$0] }.forEach(viewContext.delete)

            do {
                try viewContext.save()
            } catch {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                let nsError = error as NSError
                fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
            }
        }
    }
}

I simply created it using the default core data SwiftUI app template. Then transitioned it to using NavigationSplitView and the new NavigationLink.

I want to change the color of the selected state of the cell when I select an item in the list. Now it looks like this:

enter image description here

I want to make it so the blue selection is actually red.

Is there a way to change the selection color from the default to any color I want?

[ad_2]