How to Change the Colour of Separator in List of Swiftui

How can I change the colour of separator in list of SwiftUI?

Try this

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)

}
}

This is a kind of global variable, so you are changing the separator colour in all UITableViews from your app (List and Form are using UITableViews under the hood)

How to remove List Separator lines in SwiftUI 2.0 in iOS 14 and above

Here is a demo of possible solution. Tested with Xcode 12b.

demo

List {
ForEach(0..<3) { _ in
VStack {
Text("Hello, World!").padding(.leading)
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
.listRowInsets(EdgeInsets())
.background(Color(UIColor.systemBackground)))
}
}

How to color/remove the separator between last and second to last row in a SwiftUI List?

You can use ScrollView with ForEach instead of default List. It will be without default styling, and you can define your own style.

struct ContentView: View {
@State private var words = (1...50).map{ String($0) }
var body: some View {
GeometryReader { geometry in
ScrollView {
ForEach(self.words, id: \.self) { word in
Text(word)
}
.frame(width: geometry.size.width)
}
}
}
}

How do I modify the background color of a List in SwiftUI?

Ok, I found the solution for coloring the list rows:

struct TestRow: View {

var body: some View {
Text("This is a row!")
.listRowBackground(Color.green)
}
}

and then in body:

List {
TestRow()
TestRow()
TestRow()
}

This works as I expect, but I have yet to find out how to then remove the dividing lines between the rows...

Modify the background colour of a List in SwiftUI?

Removing ContentView1's init fixes the problem.

The issue is caused by your code setting selectedBackgroundView, but you don't need any of that init anyway as it still functions as you intend.



Related Topics



Leave a reply



Submit