4

我在 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仍然突出显示,就好像它仍然处于活动状态一样,如随附的屏幕截图所示。老实说,我不知道是什么原因造成的。非常感谢任何想法或见解。

在此处输入图像描述

4

2 回答 2

6

您可以onReceiveList:

List {
    …
}.onReceive(NotificationCenter.default.publisher(for: UITableView.selectionDidChangeNotification)) {
    guard let tableView = $0.object as? UITableView,
          let selectedRow = tableView.indexPathForSelectedRow else { return }

    tableView.deselectRow(at: selectedRow, animated: true)
}

这将取消选择选定的行。

此解决方法的最初想法归功于 Pivaisan(来自此Apple Developer thread)。

于 2021-03-13T15:45:00.470 回答
0

I had the same problem working with List, to solve this i use: UITableViewCell.appearance().selectionStyle = .none, but as you can expected you will not have the selection aspect, so the right way is try to store the state of selection and clean before leave the screen but in my trys i have no idea how to do that.

Example:

struct YourApp: App {
    
    init() {
        UITableView.appearance().backgroundColor = .clear
        UITableViewCell.appearance().selectionStyle = .none
    }

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
于 2020-12-22T16:58:45.913 回答