【问题标题】:SwiftUI NavigationLink highlight staying highlighted after returning to previous view返回上一个视图后,SwiftUI NavigationLink 突出显示保持突出显示
【发布时间】:2021-04-01 07:42:21
【问题描述】:

我在 SwiftUI 中有一系列视图。一种是“菜单视图”,它由 NavigationLinksList 组成,包裹在 NavigationView 中。

代码如下。

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

【问题讨论】:

    标签: ios swiftui swiftui-list swiftui-navigationlink swiftui-navigationview


    【解决方案1】:

    您可以对List 使用onReceive 操作:

    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)。

    【讨论】:

    • 工作就像一个魅力。非常感谢!
    • 这是我能在 Xcode 12.5 中找到的唯一适合我的解决方案
    • 不错。谢谢。
    【解决方案2】:

    我在使用 List 时遇到了同样的问题,为了解决这个问题,我使用:UITableViewCell.appearance().selectionStyle = .none,但正如您所料,您不会有选择方面,所以正确的方法是尝试存储选择状态并在之前清理离开屏幕,但在我的尝试中我不知道该怎么做。

    例子:

    struct YourApp: App {
        
        init() {
            UITableView.appearance().backgroundColor = .clear
            UITableViewCell.appearance().selectionStyle = .none
        }
    
        var body: some Scene {
            WindowGroup {
                ContentView()
            }
        }
    }
    

    【讨论】:

    • 这对我不起作用。你到底把这行代码放在哪里了?
    • 在我的@main 应用程序类中,我使用此代码创建了一个 init()。我会编辑答案。
    • 不需要您的案例背景。
    • 感谢您的回答。我的应用程序中实际上没有@main 应用程序类。我想知道这是否可以交替放置在 SceneDelegate 或 HostingController 中。否则,我会考虑创建一个应用程序类。
    • 你也可以把它放在你的appDelegate中,如果你使用uikit生命周期,不需要创建一个新项目。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多