使用SwiftUI在macOS中更改列表的突出显示颜色

时间:2020-05-17 14:01:22

标签: macos swiftui

我正在尝试使用SwiftUI为macOS制作一个简单的应用程序,我想添加一个侧边栏。我设法做到了完美,但是问题是我找不到改变List组件突出显示颜色的方法。

如您所见,所选项目为红色,但我希望它为灰色,就像在Finder之类的大多数应用程序中一样。

总而言之,有没有一种方法可以使用SwiftUI在macOS中更改列表的突出显示颜色?

请注意我没有使用Catalyst

预先感谢

我的代码

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 个答案:

没有答案