I'm trying to remove the "row" separators (known as dividers in SwiftUI) from a List
in SwiftUI.
I went through the List
documentation, but I haven't been able to find a modifier for that.
Any help would be appreciated.
In UIKit, you can easily control the appearance and color of the line separator. Unfortunately, there is no official way to remove line separators in SwiftUI. That said, we can make use of the UIKit API to tweak the line separator of the List view in SwiftUI.
Changing SwiftUI Divider Colors You can change its color by overlay it with the color you want using . overlay() modifier. Text("A visual element that can be used to separate other content.") Use overlay modifier to change the color of a divider.
This year Apple introduced a new modifier .listRowSeparator
that can be used to style the separators. you can pass .hidden
to hide it:
List { ForEach(items, id:\.self) { Text("Row \($0)") .listRowSeparator(.hidden) } }
Apple introduced LazyVStack
In iOS 14. you may consider using it instead of list for this:
ScrollView { LazyVStack { ForEach((1...100), id: \.self) { Text("Placeholder \($0)") } } }
Keep in mind that LazyVStack
is lazy and doesn't render all rows all the time. So they are very performant and suggested by Apple itself in WWDC 2020.
There is a UITableView
behind SwiftUI's List
for iOS. So to remove
you need a tableFooterView
and to remove
you need separatorStyle
to be .none
init() { // To remove only extra separators below the list: UITableView.appearance().tableFooterView = UIView() // To remove all separators including the actual ones: UITableView.appearance().separatorStyle = .none } var body: some View { List { Text("Item 1") Text("Item 2") Text("Item 3") } }
while this solution works correctly let's clean up the work using ViewModifier
public struct ListSeparatorStyleNoneModifier: ViewModifier { public func body(content: Content) -> some View { content.onAppear { UITableView.appearance().separatorStyle = .none }.onDisappear { UITableView.appearance().separatorStyle = .singleLine } } }
now let's make a small extension that would help to hide the details
extension View { public func listSeparatorStyleNone() -> some View { modifier(ListSeparatorStyleNoneModifier()) } }
As you can see, we’ve wrapped our appearance setting code into a neat little view modifier. you can declare it directly now
List { Text("1") Text("2") Text("3") }.listSeparatorStyleNone()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With