Change highlight color of List in macOS using SwiftUI

Viewed 604

I'm trying to make a simple app for macOS using SwiftUI and I'd like to add a sidebar. I've managed to make one perfectly, but the problem is that I can't find a way to change the highlight color of the List component.

As you can see, the selected item is red, but I want it to be gray, as it does on most of the applications such as Finder.

In conclusion, is there a way to change the highlight color of a List in macOS using SwiftUI?

Please note I'm not using Catalyst.

Thanks in advance

My Code:

SidebarView.swift

struct SidebarViewItem: View {

    let text: String
    let image: String

    var body: some View {
        HStack {
            if image == "Folder" || image == "Tray" {
                Image(image)
                .resizable()
                    .frame(width: 18, height: 15)
            } else if image == "Star" {
                Image(image)
                .resizable()
                    .frame(width: 20, height: 18)
            } else {
                Image(image)
                .resizable()
                    .frame(width: 20, height: 20)
            }
            Text("\(text)")
        }
    }
}

struct SidebarView: View {

    let items = ["Tray", "Folder", "Star"]

    var body: some View {

        return List {
            Text("Favorites")
                .foregroundColor(Color("sidebarAdaptiveColor"))
                .font(Font.system(size: 11, weight: .medium))

            ForEach(items, id: \.self) { item in
                NavigationLink(destination: DetailView(selection: item)) {
                    SidebarViewItem(text: "\(item)", image: item)
                }
            }.padding(.leading, 10)

            Text("Languages")
                .foregroundColor(Color("sidebarAdaptiveColor"))
                .font(Font.system(size: 11, weight: .medium))

            NavigationLink(destination: DetailView(selection: "Folder"))  {
                SidebarViewItem(text: "Hello", image: "Folder")
            }.padding(.leading, 10)
        }
        .listStyle(SidebarListStyle())
        .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
}

ContentView.swift

struct DetailView: View {

    var selection: String

    var body: some View {
        containedView()
        .frame(maxWidth: .infinity, maxHeight: .infinity)
    }

    private func containedView() -> AnyView {
        switch selection {
        case "Folder":
            return AnyView(Text(""))
        case "Star":
            return AnyView(Text("View 2"))
        case "Gear":
            return AnyView(Text("View 3"))
        default:
            return AnyView(Text("Some view here"))
        }
    }
}

struct ContentView: View {
    var body: some View {
        NavigationView {
            NotesSidebarView()
                .padding(.top)
            DetailView(selection: " Apple")
        }.navigationViewStyle(DoubleColumnNavigationViewStyle())
            .frame(minWidth: 700, maxWidth: .infinity, minHeight: 400, maxHeight: .infinity)
            .edgesIgnoringSafeArea(.top)
    }
}
0 Answers
Related