在使用 SwiftUI 的 MacOS 应用程序中,如何修改嵌套在列表中的选定 NavigationLink 的默认蓝色背景?

In a MacOS App using SwiftUI, how to modify the default blue background of a selected NavigationLink nested in a list?

大家好。在使用 SwiftUI 的 MacOS 应用程序中,如何修改嵌套在列表中的选定 NavigationLink 的默认蓝色背景?该列表位于 NavigationView 中。我在这里找不到解决方案。添加到以下基本示例的代码是什么:列出了两个 TextView,如果我们单击 TextView,我们将显示对应的 View。

ContentView.swift:

import SwiftUI

struct ContentView: View {

  @State var selection: Int?

  var body: some View {

    HStack() {
      NavigationView {
        List () {
          NavigationLink(destination: FirstView(), tag: 0, selection: self.$selection) {
            Text("Click Me To Display The First View")
          } // End Navigation Link

          NavigationLink(destination: SecondView(), tag: 1, selection: self.$selection) {
            Text("Click Me To Display The Second View")
          } // End Navigation Link

        } // End list
        .frame(minWidth: 350, maxWidth: 350)
        .onAppear {
            self.selection = 0
        }

      } // End NavigationView
        .listStyle(SidebarListStyle())
        .frame(maxWidth: .infinity, maxHeight: .infinity)

    } // End HStack
  } // End some View
} // End ContentView

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}

FirstView.swift:

import SwiftUI

struct FirstView: View {

  var body: some View {
    Text("(1) Hello, I am the first view")
      .frame(maxWidth: .infinity, maxHeight: .infinity)
  }
}

struct FirstView_Previews: PreviewProvider {
  static var previews: some View {
    FirstView()
  }
}

SecondView.swift:

import SwiftUI

struct SecondView: View {
  var body: some View {
    Text("(2) Hello, I am the second View")
      .frame(maxWidth: .infinity, maxHeight: .infinity)
  }
}

struct SecondView_Previews: PreviewProvider {
  static var previews: some View {
    SecondView()
  }
}

这是我们点击第一行的结果...选中时背景为蓝色!如何更改此默认颜色?预先感谢您的帮助。

以下代码是实现您想要的目标的一种选择:

struct ContentView: View {

@State var selection: Int? = 0
@State var pressed: Int? = 0

init() {
    UITableViewCell.appearance().selectionStyle = .none
    UITableView.appearance().backgroundColor = UIColor.clear
}

var body: some View {

    var theBinding = Binding(
        get: { self.selection },
        set: {
            self.selection = [=10=]
            self.pressed = [=10=] == nil ? self.pressed : [=10=]!
    })

    return HStack() {
        NavigationView {
            List {
                NavigationLink(destination: FirstView(), tag: 0, selection: theBinding) {
                    Text("Click Me To Display The First View")
                }.listRowBackground(self.pressed == 0 ? Color.red : Color.green)

                NavigationLink(destination: SecondView(), tag: 1, selection: theBinding) {
                    Text("Click Me To Display The Second View")
                }.listRowBackground(self.pressed == 1 ? Color.red : Color.green)

            }.frame(minWidth: 350, maxWidth: 350)
                .onAppear { self.pressed = 0 }

        }.listStyle(SidebarListStyle())
         .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
}
}