简体   繁体   English

SwiftUI 列表行在轻按后更改其背景颜色

[英]A SwiftUI list row changes its background color after a tap

I have a List with rows which when I select them, they change its row background color(UITableViewCellSelectedBackground).我有一个包含行的列表,当我选择它们时,它们会更改其行背景颜色(UITableViewCellSelectedBackground)。 I do not want this behaviour, so I would like to not change a row background color on a didSelect.我不想要这种行为,所以我不想更改 didSelect 上的行背景颜色。

I have tried to change the background color (to same color as normal state background) on tap event, but it does not work.我试图在点击事件中更改背景颜色(与正常状态背景颜色相同),但它不起作用。 Here is the current code:这是当前的代码:

var body: some View {
    List {
        ForEach (vars){ var in
            ZStack {
                Foo(a: var.name)
                    .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
                NavigationLink(destination: FooDetailView(b: var)) {
                    EmptyView()
                }.buttonStyle(PlainButtonStyle())
            }
        }.listRowBackground(Color(#colorLiteral(red: 0.03921568627, green: 0.03921568627, blue: 0.03921568627, alpha: 1)))
    }
}

This is the undesired behaviour on a tap event:这是点击事件的不良行为:

在此处输入图片说明

在此处输入图片说明

After select a row选择一行后

Copy-paste to your Playground and see the difference in first, second, third and final row ...复制粘贴到您的 Playground 并查看第一行、第二行、第三行和最后一行的差异...

import SwiftUI
import PlaygroundSupport

struct ContentView: View {
    //@State var selected
    var body: some View {
        NavigationView {
            List {
                ZStack {
                    NavigationLink(destination: Text("A")) {
                        Text("A")
                    }
                    .background(Color.yellow)
                }
                .border(Color.red)

                ZStack {
                    NavigationLink(destination: Text("B")) {
                        Text("B")
                    }
                    .background(Color.yellow)
                }
                .border(Color.red)
                .listRowInsets(EdgeInsets())

                ZStack {
                    GeometryReader { proxy in
                        NavigationLink(destination: Text("C")) {
                            Text("C").frame(height: proxy.size.height)
                        }
                        .background(Color.yellow)
                    }
                }
                .border(Color.red)
                .listRowInsets(EdgeInsets())

                ZStack {
                    GeometryReader { proxy in
                        NavigationLink(destination: Text("D")) {
                            Text("D").frame(height: proxy.size.height)
                        }
                    }
                    .background(Color.yellow.opacity(0.4))
                    .padding(.horizontal)
                }
                .background(Color.yellow.opacity(0.2))
                .listRowInsets(EdgeInsets())
            }
            .listRowBackground(Color.blue)
        }
    }
}

PlaygroundPage.current.setLiveView(ContentView())

I put some opacity there, so all the changes are better visible.我在那里放了一些不透明度,所以所有的变化都更清晰可见。

Simplified row which you can use as a template to create your own RowView(...)简化的行,您可以将其用作模板来创建自己的 RowView(...)

ZStack {
    Color.yellow
    GeometryReader { proxy in
        NavigationLink(destination: Text("D")) {
            Text("D").frame(height: proxy.size.height)
        }
    }
    .padding(.horizontal)
}
.listRowInsets(EdgeInsets())

Finally, with your own RowView() it looks like最后,用你自己的 RowView() 看起来像

ZStack {
    // background color
    Color.yellow.frame(maxWidth: .infinity)
    // your row view
    RowView()
    NavigationLink(destination: Text("D")) {
        EmptyView()
    }
    .padding(.horizontal)
}
.listRowInsets(EdgeInsets())

and some example和一些例子在此处输入图片说明

where RowView()其中 RowView()

struct RowView: View {
    var body: some View {
        HStack {
            VStack(alignment: .leading) {
                Text("Title").font(.title)
                HStack {
                    Text("detail")
                }
            }
            Spacer()
            Text("12:55").font(.title).padding()
            }.padding()
    }
}

您可以使用 tabelView.selectionStyle = .none 将此行放在 viewDidload() 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM