Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove/adjust separators in List?

Tags:

list

swiftui

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?

like image 903
Artem Abramov Avatar asked Jun 07 '19 17:06

Artem Abramov


People also ask

How to remove separator in SwiftUI list?

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.

How do I change the divider color 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.

How do I add a separator in SwiftUI?

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.

How do I create a list in SwiftUI?

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!") }


Video Answer


1 Answers

iOS 15.0+

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)
}


iOS 2.0+

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()
}

like image 130
mahan Avatar answered Oct 12 '22 21:10

mahan