7

我在 SwiftUI 中创建了一个列表。我想更改颜色或删除分隔符,因为在 UIKit 中,我们可以轻松更改 TableView 中分隔符的颜色。

下面是 SwiftUI 中列表的代码和 UI(图片)

@State private var users = ["Paul", "Taylor", "Adele"]

var body: some View {

    List(){
                ForEach(users, id: \.self) { user in
                    HStack{
                        Image("helmet").resizable().frame(width: 40, height: 40, alignment: .leading)
                        VStack(alignment : .leading){
                            Text(user).font(.custom("SFProText-Semibold", size: 14))
                            Text("Blu Connect").font(.custom("SFProText-Semibold.ttf", size: 11))
                        }
                    }
                }
                .onDelete(perform: delete)

            }
}

在此处输入图像描述

4

3 回答 3

5

试试这个

var body: some View {
        UITableView.appearance().separatorColor = UIColor.blue
        return List(){
            ForEach(users, id: \.self) { user in
                HStack{
                    Image("helmet").resizable().frame(width: 40, height: 40, alignment: .leading)
                    VStack(alignment : .leading){
                        Text(user).font(.custom("SFProText-Semibold", size: 14))
                        Text("Blu Connect").font(.custom("SFProText-Semibold.ttf", size: 11))
                    }
                }
            }
            .onDelete(perform: delete)

        }
    }

这是一种全局变量,因此您正在更改应用程序中所有 UITableViews 中的分隔符颜色(List 和 Form 在后台使用 UITableViews)

于 2019-10-26T08:44:35.337 回答
0

iOS 15

从今年开始,您可以使用以下listRowSeparatorTintColor修饰符将颜色单独应用于任何分隔符:

演示


去除

从 iOS 15 开始,您可以listRowSeparator通过传递hidden. 请阅读此详细答案以获取更多信息和示例。

于 2021-06-08T17:10:16.653 回答
0

2021 – Xcode 13 / SwiftUI 3

更改分隔符的颜色:

.listRowSeparatorTint(Color.pink)

隐藏分隔符:

.listRowSeparator(.hidden)
于 2021-06-09T19:50:53.520 回答