如何更改列表中导航 link 的 select 颜色
How do you change the select color of a navigation link in a list
这看起来像是一个简单的问题,但我在 google 上的任何地方都找不到答案。
当导航 link 在列表中时,您可以将其颜色更改为蓝色:
.listRowBackground(Color.blue)
但是当导航link被select编辑时,它变成了systemGray4
如何更改列表link中导航select的颜色
这是上图中的示例项目:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
List {
NavigationLink("Hello", destination: Text("hello"))
.listRowBackground(Color.blue)
NavigationLink("World", destination: Text("world"))
.listRowBackground(Color.blue)
}
}
}
}
您可以更改 UITableViewCell.appearance()
的 属性 .selectionStyle
。
在视图的 init
中或列表出现时:
.onAppear {
UITableViewCell.appearance().selectionStyle = .none
}
并继续使用 listRowBackground
管理链接的突出显示颜色
如果您跟踪所选选项卡,则可以专门为所选选项卡设置列表行的颜色。
示例:
struct ContentView: View {
enum Tab: String, CaseIterable, Identifiable {
case hello
case world
var id: String { rawValue }
var title: String {
switch self {
case .hello: return "Hello"
case .world: return "World"
}
}
}
@State private var selectedTab: Tab?
var body: some View {
NavigationView {
List {
ForEach(Tab.allCases) { tab in
NavigationLink(tab.title, tag: tab, selection: $selectedTab) {
Text(tab.rawValue)
}
.listRowBackground(tab == selectedTab ? Color.accentColor : nil)
}
}
}
}
}
在模拟器上录制gif时,不知为何无法正常使用。不确定这是否是错误。
这看起来像是一个简单的问题,但我在 google 上的任何地方都找不到答案。
当导航 link 在列表中时,您可以将其颜色更改为蓝色:
.listRowBackground(Color.blue)
但是当导航link被select编辑时,它变成了systemGray4
如何更改列表link中导航select的颜色
这是上图中的示例项目:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
List {
NavigationLink("Hello", destination: Text("hello"))
.listRowBackground(Color.blue)
NavigationLink("World", destination: Text("world"))
.listRowBackground(Color.blue)
}
}
}
}
您可以更改 UITableViewCell.appearance()
的 属性 .selectionStyle
。
在视图的 init
中或列表出现时:
.onAppear {
UITableViewCell.appearance().selectionStyle = .none
}
并继续使用 listRowBackground
如果您跟踪所选选项卡,则可以专门为所选选项卡设置列表行的颜色。
示例:
struct ContentView: View {
enum Tab: String, CaseIterable, Identifiable {
case hello
case world
var id: String { rawValue }
var title: String {
switch self {
case .hello: return "Hello"
case .world: return "World"
}
}
}
@State private var selectedTab: Tab?
var body: some View {
NavigationView {
List {
ForEach(Tab.allCases) { tab in
NavigationLink(tab.title, tag: tab, selection: $selectedTab) {
Text(tab.rawValue)
}
.listRowBackground(tab == selectedTab ? Color.accentColor : nil)
}
}
}
}
}
在模拟器上录制gif时,不知为何无法正常使用。不确定这是否是错误。