带有 NavigationLink 的 SwiftUI 列表如何在点击时自定义突出显示

Mic*_*bro 3 list swiftui navigationlink

我如何在列表行上制作自定义叠加层,以便在点击时突出显示它。我正在使用NaviagationLink并且我已经改变了

UITableViewCell.appearance().cellSelectionStyle = .none 
Run Code Online (Sandbox Code Playgroud)

为了不使用默认的灰色选择。

我试图向@State isSelected我的 Cell添加一些,但我不知道如何/何时更改它以获得这种影响。我试图添加,onTapGesture()但它阻止了 NavigationLink,并且不提供开始、结束状态,刚刚结束。

Mic*_*bro 5

我这样做:

List {
    ForEach(0..<self.contacts.count) { i in
       ZStack {
           NavigationLink(destination: ContactDetails(contact: self.contacts[i])) {
                EmptyView()
           }.opacity(0)

           Button(action: {}) {
                   ContactRow(contact: self.contacts[i])
           }.buttonStyle(ListButtonStyle())
        }.listRowBackground( (i%2 == 0) ? Color("DarkRowBackground") : .white)
           .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
     }
}
Run Code Online (Sandbox Code Playgroud)

并且ButtonStyle

struct ListButtonStyle: ButtonStyle {

    func makeBody(configuration: Self.Configuration) -> some View {

        configuration.label
            .overlay(configuration.isPressed ? Color("Dim").opacity(0.4) : Color.clear)
    }
}
Run Code Online (Sandbox Code Playgroud)


Asp*_*eri 3

好的,这是该方法的非常简单的演示。我们的想法是将 NavigationLink 保留在列表之外,并在行点击时手动激活它(无需内部导航链接即可轻松点击)...其他所有内容(例如动画、效果和突出显示类型)都取决于您。

import SwiftUI
import UIKit

struct TestCustomCellHighlight: View {
    @State var selection: Int = -1
    @State var highlight = false
    @State var showDetails = false

    init() {
        UITableViewCell.appearance().selectionStyle = .none
    }

    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: Text("Details \(self.selection)"), isActive: $showDetails) {
                    EmptyView()
                }
                List(0..<20, id: \.self) { i in
                    HStack {
                        Text("Item \(i)")
                        Spacer()
                    }
                    .padding(.vertical, 6)
                    .background(Color.white) // to be tappable row-wide
                    .overlay(self.highlightView(for: i))
                    .onTapGesture {
                        self.selection = i
                        self.highlight = true

                        // delay link activation to see selection effect
                        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
                            self.highlight = false
                            self.showDetails = true
                        }
                    }
                }
            }
        }
    }

    private func highlightView(for index: Int) -> AnyView {
        if self.highlight && self.selection == index {
            return AnyView(Rectangle().inset(by: -5).fill(Color.red.opacity(0.5)))
        } else {
            return AnyView(EmptyView())
        }
    }
}

struct TestCustomCellHighlight_Previews: PreviewProvider {
    static var previews: some View {
        TestCustomCellHighlight()
    }
}
Run Code Online (Sandbox Code Playgroud)