如何更改SwiftUI列表中分隔符的颜色?

时间:2019-10-25 13:27:50

标签: swift user-interface swiftui ios13 swiftui-list

我在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)

            }
}

enter image description here

3 个答案:

答案 0 :(得分:0)

尝试

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)

答案 1 :(得分:0)

改变颜色

从 iOS 15 开始,您可以使用 listRowSeparatorTintColor 修饰符单独将颜色应用于任何分隔符,例如:

Demo


删除

从 iOS 15 开始,您可以通过传递 listRowSeparator 来使用 hidden 修饰符。 请阅读this detailed answer了解更多信息和示例。

答案 2 :(得分:0)

2021 – Xcode 13 / SwiftUI 3

更改分隔符的颜色:

.listRowSeparatorTint(Color.pink)

隐藏分隔符:

.listRowSeparator(.hidden)