我在 SwiftUI 中有一系列视图。一种是“菜单视图”,它由封装在 NavigationView 中的NavigationLinks列表组成。
代码如下。
var body: some View {
NavigationView {
List {
HStack {
NavigationLink(destination: History(), isActive: $isHistoryViewActive) {
Image(systemName: "clock")
Text("History")
}
}
HStack {
NavigationLink(destination: Settings(), isActive: $isSettingsActive) {
Image(systemName: "gear")
Text("Settings")
}
}
HStack {
Image(systemName: "info.circle.fill")
Button(action: {
...
}) {
Text("My Button")
}
}
}
}
}
设置视图如下
var body: some View {
List {
...
Section(header: "Background Music") {
Toggle("Play", isOn: $isBackGroundMusicOn)
}
Section(header: "Voice Setting") {
HStack {
NavigationLink(destination: VoiceList() {
Text(self.voiceNames[self.selectedVoice])
}
}
}
}
最后,VoiceList视图如下:
var body: some View {
List {
ForEach(0 ..< VoiceList.voiceNames.count) {voiceIndex in
HStack {
Button(action: {
voiceChanged(selectedVoice: voiceIndex)
}){
Text(VoiceList.voiceNames[voiceIndex])
}
Spacer()
Image(systemName: "checkmark")
.frame(alignment: .trailing)
.foregroundColor(.blue)
.isHidden(hidden: voiceIndex != selectedVoice)
}
}
}
}
我遇到的问题是,当应用程序从VoiceList视图返回到Settings视图时,NavigationLink仍然突出显示,就好像它仍然处于活动状态一样,如随附的屏幕截图所示。老实说,我不知道是什么原因造成的。非常感谢任何想法或见解。