带有NavigationLink的SwiftUI列表如何在点击时自定义突出显示 [英] SwiftUI List with NavigationLink how to make custom highlight on tap

查看:560
本文介绍了带有NavigationLink的SwiftUI列表如何在点击时自定义突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在列表行上自定义重叠,以便在点击时将其突出显示.我正在使用 NaviagationLink ,并且已经更改了

How I can make custom overlay over List row that will highlight it on tap. I am using NaviagationLink and I've changed

UITableViewCell.appearance().cellSelectionStyle = .none 

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

in order to not use default gray selection.

我尝试向我的单元格添加一些@State isSelected,但是我不知道如何/何时更改此值以产生这种影响.我尝试添加onTapGesture(),但是它阻止了NavigationLink,并且不提供开始,结束状态,刚刚结束.

I've tried to add some @State isSelected to my Cell but I do not know how/when to change this to get this affect. I've tried to add onTapGesture() but it prevents NavigationLink, and doesn't provide begin, ended states, just ended.

推荐答案

我这样做:

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

ButtonStyle:

struct ListButtonStyle: ButtonStyle {

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

        configuration.label
            .overlay(configuration.isPressed ? Color("Dim").opacity(0.4) : Color.clear)
    }
}

这篇关于带有NavigationLink的SwiftUI列表如何在点击时自定义突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆