macos - 在使用 SwiftUI 的 MacOS 应用程序中,如何修改嵌套在列表中的选定 NavigationLink 的默认蓝色背景?
问题描述
美好的一天。在使用 SwiftUI 的 MacOS 应用程序中,如何修改嵌套在列表中的选定 NavigationLink 的默认蓝色背景?该列表位于 NavigationView 内。我在这里找不到解决方案。添加到以下基本示例的代码是什么:列出了两个 TextView,如果我们单击 TextView,我们会显示相应的 View。
内容视图.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 = $0
self.pressed = $0 == nil ? self.pressed : $0!
})
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)
}
}
}
推荐阅读
- javascript - 是否可以在快速路由器中使用应用程序级中间件?
- java - 如何使用简单的 json 读取 Java 中的 JSON 文件?
- react-native - Expo React Native - 如何使用 ErrorRecovery?
- postgresql - 并发创建唯一索引时的 Postgres lock_timeout
- javascript - 带有 d3.js(v5)色标的谷歌地图不起作用
- python - 如何在 Django 中减去两个日期?
- javascript - ESlint 和函数或 const 中的对象扩展运算符,为什么会抱怨?
- templates - 需要使用 For 循环而不是 Azure Pipeline Template yaml 中的每个循环
- jenkins - 在多个模块上生成三叶草报告
- react-native - Shoutem cloud - 从 react-native 部分添加新项目