Is there a way to remove separators or adjust separator insets in List
view in SwiftUI?
In UIKit it can be achieved through
tableView.separatorStyle = .none
and
tableview.separatorInset = UIEdgeInsets(top: 0, left: 18, bottom: 0, right: 18)
What are the corresponding SwiftUI alternatives?
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.
If the canvas isn't visible, select Editor > Editor and Canvas to show it. The Horizontal Divider is displayed as a horizontal line to divide views. Inside a HStack the divider is displayed as a vertical line. The size of the divider can be changed with the frame modifier.
Probably the simplest way to build a list is to create a new SwiftUI view and wrap the Hello World text in a List: struct StaticListView: View { var body: some View { List { Text("Hello, world!") } } } To add more items to the list, we can just add another line: List { Text("Hello, world!") Text("Hello, SwiftUI!") }
Mac Catalyst 15.0+
listRowSeparator(_:edges:)
Sets the display mode for the separator associated with this specific row. https://developer.apple.com/
List {
ForEach(0..<10, id: \.self) { number in
Text("Text\(number)")
}.listRowSeparator(.hidden)
}
struct ListRowSeperatorModifier: ViewModifier {
func body(content: Content) -> some View {
if #available(iOS 15.0, *) {
content.listRowSeparator(.hidden)
} else {
content.onAppear {
UITableView.appearance().separatorStyle = .none
}
.onDisappear {
UITableView.appearance().separatorStyle = .singleLine
}
}
}
}
extension View {
func hideListRowSeparator() -> some View {
return self.modifier(ListRowSeperatorModifier())
}
}
Use .hideListRowSeparator()
on ForEach
.
List {
ForEach(0..<10, id: \.self) { number in
Text("Text\(number)")
}.hideListRowSeparator()
}
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